You can use the HTTpReq class:
public class HttpReq { public String send (String url){ //send a http request and get the result InputStream is = null; String result = ""; try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); }catch(Exception e){ Log.e("log_tag", "Error in http connection " + e.toString()); } try { BufferedReader reader = new BufferedReader(new InputStreamReader(is),8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error converting result " + e.toString()); } return result; }
}
Then you use this class to connect and to call your php file to get data as JSonObject.
ht = new HttpReq(); // send a http request with GET x=ht.send("http://10.0.2.2/myFolder/myFile.php"); JSONArray jArray; JSONObject json_data; String h[]=x.split("<"); try { jArray = new JSONArray(h[0]); json_data = jArray.getJSONObject(0); url=json_data.getString("url"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }
In your Php file, you can use these methods to get JSon data or send it to an Android application.
Json_decode($string);
and
Json_encode($string);
I hope this helps you :)
source share