我在我的 Android 应用程序中使用 JSON 进行帐户注册时遇到问题。这是我在调试后发现的有问题的部分:
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
registerErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully registered
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Registration Screen
finish();
}else{
// Error in registration
registerErrorMsg.setText("Error occured in registration");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
完整的 LogCat:
12-09 14:31:02.360: D/dalvikvm(5339): GC_EXTERNAL_ALLOC 释放 118K, 52% 空闲 2666K/5447K, 外部 943K/1034K, 暂停 30ms 12-09 14:31:08.735: W/System.err(5339 ): java.net.SocketException: Permission denied 12-09 14:31:08.735: W/System.err(5339): at org.apache.harmony.luni.platform.OSNetworkSystem.socket(Native Method) 12-09 14 :31:08.735: W/System.err(5339): 在 dalvik.system.BlockGuard$WrappedNetworkSystem.socket(BlockGuard.java:335) 12-09 14:31:08.740: W/System.err(5339): 在org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:216) 12-09 14:31:08.740: W/System.err(5339): at java.net.Socket.checkOpenAndCreate(Socket.java :821) 12-09 14:31:08.740: W/System.err(5339): 在 java.net.Socket.connect(Socket.java:967) 12-09 14:31:08.740: W/System.err (5339):在 org.apache.http.conn.scheme.PlainSocketFactory。connectSocket(PlainSocketFactory.java:119) 12-09 14:31:08.740: W/System.err(5339): 在 org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:156) 12-09 14:31:08.740: W/System.err(5339): 在 org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 12-09 14:31:08.740: W/System.err (5339): 在 org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 12-09 14:31:08.740: W/System.err(5339): 在 org.apache.http。 impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359) 12-09 14:31:08.740: W/System.err(5339): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java :555) 12-09 14:31:08.740: W/System.err(5339): 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 12-09 14:31:08.740: W/System.err(5339): 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 12-09 14:31: 08.740: W/System.err(5339): 在 com.example.musicshare.library.JSONParser.getJSONFromUrl(JSONParser.java:42) 12-09 14:31:08.740: W/System.err(5339): 在 com .example.musicshare.library.UserFunctions.registerUser(UserFunctions.java:59) 12-09 14:31:08.740: W/System.err(5339): at com.example.musicshare.RegisterActivity$1.onClick(RegisterActivity.java :55) 12-09 14:31:08.740: W/System.err(5339): 在 android.view.View.performClick(View.java:2538) 12-09 14:31:08.740: W/System.err (5339): 在 android.view.View$PerformClick.run(View.java:9152) 12-09 14:31:08.740: W/System.err(5339): 在 android.os.Handler.handleCallback(Handler. java:587) 12-09 14:31:08.740: W/System.err(5339):在 android.os.Handler.dispatchMessage(Handler.java:92) 12-09 14:31:08.740: W/System.err(5339): 在 android.os.Looper.loop(Looper.java:123) 12- 09 14:31:08.760: W/System.err(5339): 在 android.app.ActivityThread.main(ActivityThread.java:3691) 12-09 14:31:08.760: W/System.err(5339): 在java.lang.reflect.Method.invokeNative(Native Method) 12-09 14:31:08.760: W/System.err(5339): 在 java.lang.reflect.Method.invoke(Method.java:507) 12- 09 14:31:08.760: W/System.err(5339): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 12-09 14:31:08.760: W/System。错误(5339):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)12-09 14:31:08.760:W/System.err(5339):在 dalvik.system.NativeStart.main (本机方法)12-09 14:31:08.760:E/Buffer 错误(5339):转换结果 java.lang 时出错。NullPointerException 12-09 14:31:08.760:E/JSON Parser(5339):解析数据 org.json.JSONException 时出错:12-09 14:31:08.760 字符 0 处的输入结束:D/AndroidRuntime(5339):关闭 VM 12-09 14:31:08.760: W/dalvikvm(5339): threadid=1: 线程退出未捕获异常 (group=0x4001e578) 12-09 14:31:08.765: E/AndroidRuntime(5339): FATAL例外:主要 12-09 14:31:08.765:E/AndroidRuntime(5339):java.lang.NullPointerException 12-09 14:31:08.765:E/AndroidRuntime(5339):在 com.example.musicshare.RegisterActivity$1。 onClick(RegisterActivity.java:59) 12-09 14:31:08.765: E/AndroidRuntime(5339): 在 android.view.View.performClick(View.java:2538) 12-09 14:31:08.765: E/ AndroidRuntime(5339):在 android.view.View$PerformClick.run(View.java:9152) 12-09 14:31:08.765:E/AndroidRuntime(5339):在 android.os.Handler。handleCallback(Handler.java:587) 12-09 14:31:08.765: E/AndroidRuntime(5339): 在 android.os.Handler.dispatchMessage(Handler.java:92) 12-09 14:31:08.765: E/ AndroidRuntime(5339): 在 android.os.Looper.loop(Looper.java:123) 12-09 14:31:08.765: E/AndroidRuntime(5339): 在 android.app.ActivityThread.main(ActivityThread.java:3691 ) 12-09 14:31:08.765: E/AndroidRuntime(5339): at java.lang.reflect.Method.invokeNative(Native Method) 12-09 14:31:08.765: E/AndroidRuntime(5339): at java. lang.reflect.Method.invoke(Method.java:507) 12-09 14:31:08.765: E/AndroidRuntime(5339): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847 ) 12-09 14:31:08.765: E/AndroidRuntime(5339): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 12-09 14:31:08.765: E/AndroidRuntime(5339 ): 在 dalvik.system.NativeStart。main(Native Method) 12-09 14:31:08.800: D/dalvikvm(5339): GC_CONCURRENT freed 208K, 51% free 2789K/5639K, external 1350K/1690K, paused 3ms+4ms
所以这是我的 JSON 解析代码:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}