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<jsonArray.length();i++)
{
JSONObject jsonObject = null;
try {
jsonObject = jsonArray.getJSONObject(i);
s = s +
"Name : "+jsonObject.getString("name")+"\n"
+ "Age : "+jsonObject.getString("age")+"\n"
+ "Address : "+jsonObject.getString("address")+"\n\n";
}
catch (JSONException e)
{
e.printStackTrace();
}
}
this.responseTextView.setText(s);
}
private class GetAllCustomerTask extends AsyncTask<ApiConnector,Long,JSONArray>
{
@Override
protected JSONArray doInBackground(ApiConnector... params) {
//this execute a bg thread
return params[0].getAllCustomers();
}
protected void onPostExecute(JSONArray jsonArray)
{
setTextToTextView(jsonArray);
}
}
}
-
**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;
}
}
Here my simple layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/responseTextView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="60dp"
android:textAlignment="center"/>
</RelativeLayout>
Along with that my test.php file.
<?php
$con = mysqli_connect("localhost","root","root","My_Database");
if(!$con)
{
die("Could not connect : ".mysql_error());
}
/*mysqli_select_db("My_Database",$con);*/
$result = mysqli_query($con,"SELECT * From Customer");
while($row=mysqli_fetch_assoc($result))
{
$output[] = $row;
}
print(json_encode($output));
mysqli_close($con);
?>
One more thing I have considered that HttpEntity, DefaultHttpClient, HttpGet, HttpResponse, ClientProtocolException, EntityUtils Above all are deprecated