【发布时间】:2014-02-02 15:09:17
【问题描述】:
我正在尝试访问 Web API 服务器并将 json 对象传递给它。问题是,我总是遇到这个异常。我真的不知道我的代码有什么问题。这里是:
String email = EmailAddress.getText().toString();
String password = Password.getText().toString();
String url = "SOME URL";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
JSONObject loginObj = new JSONObject();
try
{
loginObj.put("Username", email);
loginObj.put("Password", password);
// Prepare JSON to send by setting the entity
httpPost.setEntity(new StringEntity(loginObj.toString(), "UTF-8"));
// Set up the header types needed to properly transfer JSON
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept-Encoding", "application/json");
httpPost.setHeader("Accept-Language", "en-US");
// Execute POST
HttpResponse response = httpClient.execute(httpPost);
Log.i("Response String", response.getEntity().toString());
}
catch(Exception e)
{
Log.e("Exception", e.getMessage().toString());
}
这是我的日志:
02-02 23:06:37.080: E/AndroidRuntime(26513): FATAL EXCEPTION: main
02-02 23:06:37.080: E/AndroidRuntime(26513): java.lang.IllegalStateException: Could not execute method of the activity
02-02 23:06:37.080: E/AndroidRuntime(26513): at android.view.View$1.onClick(View.java:3691)
02-02 23:06:37.080: E/AndroidRuntime(26513): at android.view.View.performClick(View.java:4211)
02-02 23:06:37.080: E/AndroidRuntime(26513): at android.view.View$PerformClick.run(View.java:17267)
02-02 23:06:37.080: E/AndroidRuntime(26513): at android.os.Handler.handleCallback(Handler.java:615)
02-02 23:06:37.080: E/AndroidRuntime(26513): at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 23:06:37.080: E/AndroidRuntime(26513): at android.os.Looper.loop(Looper.java:137)
02-02 23:06:37.080: E/AndroidRuntime(26513): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-02 23:06:37.080: E/AndroidRuntime(26513): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 23:06:37.080: E/AndroidRuntime(26513): at java.lang.reflect.Method.invoke(Method.java:511)
02-02 23:06:37.080: E/AndroidRuntime(26513): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-02 23:06:37.080: E/AndroidRuntime(26513): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-02 23:06:37.080: E/AndroidRuntime(26513): at dalvik.system.NativeStart.main(Native Method)
02-02 23:06:37.080: E/AndroidRuntime(26513): Caused by: java.lang.reflect.InvocationTargetException
02-02 23:06:37.080: E/AndroidRuntime(26513): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 23:06:37.080: E/AndroidRuntime(26513): at java.lang.reflect.Method.invoke(Method.java:511)
02-02 23:06:37.080: E/AndroidRuntime(26513): at android.view.View$1.onClick(View.java:3686)
02-02 23:06:37.080: E/AndroidRuntime(26513): ... 11 more
02-02 23:06:37.080: E/AndroidRuntime(26513): Caused by: java.lang.NullPointerException
02-02 23:06:37.080: E/AndroidRuntime(26513): at com.example.maddiosapp.Login.userLogin(Login.java:81)
02-02 23:06:37.080: E/AndroidRuntime(26513): ... 14 more
有什么想法吗?谢谢!
【问题讨论】:
-
Login.java 文件的第 81 行是什么?
-
Log.e("异常", e.getMessage().toString()); -- 这个
-
我把这个改成了吐司。现在它可以工作了。但我不知道是什么导致了异常。我希望我能用 e.getMessage() 得到它。有什么想法吗?
标签: android json http-post httpclient