【发布时间】:2015-07-06 12:22:24
【问题描述】:
我是 android 编程新手,我可能只是错过了一个明显的错误,但我现在看不到它。
重点其实很简单。我有一个有效的 HttpGet 方法,只是为了尝试我将它更改为 HttpPost 方法。但没有成功。
有趣的部分是“lastTweet”方法。我刚刚评论了 Post-Request,但就像我说的,如果我从 get 更改为 post,应用程序就会崩溃。
因此,如果有人能解释我做错了什么,我将不胜感激。 谢谢。
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);
}
}
}
感谢您的快速回复。 根据 greenapps 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)
【问题讨论】:
-
如果应用程序崩溃,您会在 logcat 中找到原因。请张贴在这里。
-
刚刚添加了logcat。谢谢你的帮助
标签: android http-post http-get androidhttpclient