While trying with client server (fetching mysql data) with help of a tutorial I get many errors. I just read some threads here and I understood that the biggest error I get
Error in http connectionandroid.os.NetworkOnMainThreadException E/log_tag: Error converting result java.lang.NullPointerException
might be because I need a helper task between thread and handler.
I have the following code, I understand this concept and normally it should work.
package de.irgendwas;
import android.app.Activity;
import android.net.ParseException;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
public class MainActivity extends Activity {
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** Called when the activity is first created. */
String result = null;
InputStream is = null;
StringBuilder sb = null;
//http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/connect_db.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection established");
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
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());
}
//paring data
int fd_id = 0;
String fd_name = "";
try {
JSONArray jArray = new JSONArray(result);
JSONObject json_data = null;
Toast.makeText(getBaseContext(), "1. try Block", Toast.LENGTH_LONG).show();
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
fd_id = json_data.getInt("artist");
fd_name = json_data.getString("title");
}
} catch (JSONException e1) {
Toast.makeText(getBaseContext(), "No Data Found", Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
//print received data
TextView t = (TextView) findViewById(R.id.text);
t.setText("ID: " + fd_id + " Name: " + fd_name);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
public Action getIndexApiAction() {
Thing object = new Thing.Builder()
.setName("Main Page") // TODO: Define a title for the content shown.
// TODO: Make sure this auto-generated URL is correct.
.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
AppIndex.AppIndexApi.start(client, getIndexApiAction());
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
AppIndex.AppIndexApi.end(client, getIndexApiAction());
client.disconnect();
}
}
I won't post the php script on the server, because if I call the URL via browser it delivers my testdata properly, so it should be fine.
Now while executing the app I get the errors above. While searching the web I found the DownloadFilesTask concept, it sounds like it should be the solution. But I won't get it, I'm not able to implement it in my code. Can somebody help or is there a good tutorial somewhere besides the one on developer.android.
The error happens because you want to do a networking related process in UI Thread. You need to move it away from your UI Thread. For your case, you can use AsyncTask.
AsyncTask is a mechanism for executing operations in a background thread without having to manually handle thread creation or execution. AsyncTasks were designed to be used for short operations (a few seconds at the most) and you might want to use a Service and/or Executor for very long running tasks.
AsyncTask is typically used for long running tasks that cannot be done on UI Thread, such as downloading network data from an API or indexing data from elsewhere on the device.
Please read Creating and Executing Async Tasks.
Here an example for using AsyncTask in network related process: