i'm new in android programming and may i just missed an obvious mistake there but i don't see it right now.
The point is actually pretty simple. I have a working HttpGet Method and just for trying i changed it into a HttpPost Method. But it didn't work out.
The interesting part is the "lastTweet" Method. I just commented the Post-Request, but like i said, if i'm changing from the get to the post the app crashes.
So i would be really grateful if someone could explain me what i did wrong. Thank you.
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
TextView Text;
HttpClient client;
JSONObject json;
final static String URL = "http://www.openligadb.de/api/getmatchdata/bl1/2014/15";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Text = (TextView)findViewById(R.id.myTextView);
client = new DefaultHttpClient();
new Read().execute("MatchID");
}
public JSONObject lastTweet(String username) throws ClientProtocolException, IOException, JSONException {
HttpGet get = new HttpGet(URL);
//HttpPost post = new HttpPost(URL);
/*
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("key1", "value1"));
pairs.add(new BasicNameValuePair("key2", "value2"));
post.setEntity(new UrlEncodedFormEntity(pairs));
*/
HttpResponse r = client.execute(get);
//HttpResponse r = client.execute(post);
int status = r.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONArray timeline = new JSONArray(data);
JSONObject last = timeline.getJSONObject(0);
return last;
}
else {
Toast.makeText(MainActivity.this, "error", Toast.LENGTH_LONG).show();
return null;
}
}
public class Read extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... params) {
try {
json = lastTweet("wurst");
return json.getString(params[0]);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
Text.setText(s);
}
}
}
Thanks for the quick response. As asked from greenapps the Logcat:
Local Branch:
Remote Branch:
Local Patches:
Reconstruct Branch:
04-29 16:55:02.764 2642-2642/com.example.itsme.jsonnb2 D/OpenGLRenderer﹕ Enabling debug mode 0
04-29 16:55:04.116 2642-2855/com.example.itsme.jsonnb2 W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x418a1da0)
04-29 16:55:04.146 2642-2855/com.example.itsme.jsonnb2 E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.itsme.jsonnb2, PID: 2642
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.widget.Toast$TN.<init>(Toast.java:384)
at android.widget.Toast.<init>(Toast.java:113)
at android.widget.Toast.makeText(Toast.java:272)
at com.example.itsme.jsonnb2.MainActivity.lastTweet(MainActivity.java:68)
at com.example.itsme.jsonnb2.MainActivity$Read.doInBackground(MainActivity.java:78)
at com.example.itsme.jsonnb2.MainActivity$Read.doInBackground(MainActivity.java:73)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)