Android , PHP and MySQL

0 Swapnil Srivastav · June 21, 2015
 I am beginner in Android here I am attempting to connect my Android application to a simple php which is returning JSON objects.

Whenever I am running my application I am getting error unfortunately app has stopped on emulator and in ABD LOG I am getting PropertyFetcher: AdbCommandRejectedException getting properties for device emulator-5554: device offline.



**MainActivity.java**
---------------------


package jsonphp.com.jsonphp;

import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class MainActivity extends ActionBarActivity {

   private TextView responseTextView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       this.responseTextView = (TextView) this.findViewById(R.id.responseTextView);

       new GetAllCustomerTask().execute(new ApiConnector());

   }



   public void setTextToTextView(JSONArray jsonArray)
   {
       String s = "";
       for(int i=0;i




**ApiConnector.java**
---------------------
package jsonphp.com.jsonphp;

import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;

import java.io.IOException;

/**
* Created by SWAPNIL on 20-06-2015.
*/
public class ApiConnector {

   public JSONArray getAllCustomers()
   {
       //URL for getting all customers

       String URL = "http://127.0.0.1/Android/test.php";

       // Get HttpResponse object from URL
       // Get HttpEntity from HTTP Response object
       HttpEntity httpEntity = null;
       try {
           DefaultHttpClient httpClient = new DefaultHttpClient();
           HttpGet httpGet = new HttpGet(URL);

           HttpResponse httpResponse = httpClient.execute(httpGet);
           httpEntity = httpResponse.getEntity();
       }
       catch (ClientProtocolException ex)
       {
                   ex.printStackTrace();
       }
       catch (IOException e)
       {
           e.printStackTrace();
       }


       //Converting HttpEntity into JSONArray

       JSONArray jsonArray = null;
       if(httpEntity != null)
       {
           try {
               String entityResponse = EntityUtils.toString(httpEntity);
               Log.e("Entity Response : ",entityResponse);
               jsonArray = new JSONArray(entityResponse);
           }
           catch (JSONException ee)
           {
               ee.printStackTrace();
           }
           catch (IOException e)
           {
               e.printStackTrace();
           }
       }
       return  jsonArray;
   }


}


One more thing I have considered that HttpEntity, DefaultHttpClient, HttpGet, HttpResponse, ClientProtocolException, EntityUtils Above all are deprecated.
Here my simple layout.



   




My test.php which is returning JSON.


[/code]

Post a Reply

Replies

Oldest  Newest  Rating
+1 Swapnil Srivastav · June 21, 2015
Issue resolved !!!!
  • 1

Java / Android Development

107,175 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator