我是Android编程的新手,我可能只是错过了一个明显的错误,但我现在还没有看到它。
这一点实际上非常简单。我有一个工作的HttpGet方法,只是为了尝试我将其改为HttpPost方法。但它没有成功。
有趣的部分是" lastTweet"方法。我只是评论了Post-Request,但就像我说的,如果我从应用程序更改为帖子,应用程序崩溃。
如果有人能解释我做错了什么,我真的很感激。 谢谢。
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)
答案 0 :(得分:0)
在doInBackGround中,您无法使用Toast()。你在lastTweet()中使用Toast(),因此在doInBackGround中使用。将Toast bij替换为Log.d()。您还可以在AsyncTask的私有String中留言,并在.. Toast()中的onPostExecute中显示该字符串。研究logcat,你会看到。
json = lastTweet("wurst");
return json.getString(params[0]);
因为lastTweet()可以返回null,因此json == null以下语句json.getString(params [0]);将导致NullPointerException。所以当json == null时不要执行该语句。
更改为
json = lastTweet("wurst");
if ( json == null )
return null;
return json.getString(params[0]);
在onPostExcute参数中,s可以为null。因此,检查该参数是否为null,并显示一个toast,如果为null,则显示为。