我在使用JSON在我的Android應用程序中進行帳戶註冊時遇到問題。下面是我調試後發現有問題的一部分:在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:權限被拒絕 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):at dalvik.system.BlockGuard $ WrappedNetworkSystem。 socket(BlockGuard.java:335) 12-09 14:31:08.740:W/System.err(5339):at 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):at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119) 12-09 14:31:08.740:W/System。 err(5339):at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:156) 12-09 14:31:08.740:W/System.err(5339):at org.apache。 http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 12-09 14:31:08.740:W/System.err(5339):at org.apache.http.impl.conn.AbstractPooledConnAdapter.open( AbstractPoole dConnAdapter.java:119) 12-09 14:31:08.740:W/System.err(5339):at 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):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 12-09 14:31:08.740:W/System.err(5339):at org.apache。 http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 12-09 14:31:08.740:W/System.err(5339):at com.example.musicshare.library.JSONParser.getJSONFromUrl(JSONParser。 java:42) 12-09 14:31:08.740:W/System.err(5339):at 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):at android.view.View.performClick(View.java:2538) 12-09 14:31:08.740:W/System.err(5339):at android.view.View $ PerformClick.run (View.java:9152) 12-09 14:31:08.740:W/System.err(5339):at android.os.Handler.handleCallback(Handler.java:587) 12-09 14:31:08.740 :W/System.err(5339):at android.os.Handler.dispatchMessage(Handler.java:92) 12-09 14:31:08.740:W/System.err(5339):at android.os.Looper .loop(Looper.java:123) 12-09 14:31:08.760:W/System.err(5339):at android.app.ActivityThread.main(ActivityThread.java:3691) 12-09 14:31 :08.760:W/System.err(5339):在java.lang.reflect.Method.invokeNati ve(Native Method) 12-09 14:31:08.760:W/System.err(5339):at java.lang.reflect.Method.invoke(Method.java:507) 12-09 14:31:08.760 :W/System.err(5339):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:847) 12-09 14:31:08.760:W/System.err(5339): (com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 12-09 14:31:08.760:W/System.err(5339):at dalvik.system.NativeStart.main(Native Method) E/JSON解析器(5339):解析數據org.json時出錯()12-09 14:31:08.760:E/Buffer錯誤(5339):轉換結果出錯java.lang.NullPointerException 12-09 14:31:08.760:E/JSON解析器。JSONException:在 12-09 14:31:08.760 D/AndroidRuntime(5339):字符0處的輸入結束時關閉VM 12-09 14:31:08.760:W/dalvikvm(5339):threadid = 1:線程退出與未捕獲的異常(組= 0x4001e578) 12-09 14:31:08.765:E/AndroidRuntime(5339):致命例外:主 12-09 14:31:08.765:E/AndroidRuntime(5339):java。 lang.NullPointerException 12-09 14:31:08.765:E/AndroidRuntime(5339):at 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):at android.view.View $ PerformClick.run(View。 java:9152) 12-09 14:31:08.765:E/AndroidRuntime(5339):at android.os.Handler.han dleCallback(Handler.java:587) 12-09 14:31:08.765:E/AndroidRuntime(5339):at 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):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:847) 12-09 14:31:08.765:E/AndroidRuntime(5339):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 12-09 14:31:08.765:E/AndroidRuntime(5339):at dalvik.system.NativeStart.main(Native Method) 12-09 14:31:08.800:D/dalvikvm(5339):GC_CONCURRENT已釋放208K, 51%免費2789K/5639K,外部1350K/1690K,暫停3毫秒+ 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;
}
}
那麼問題是什麼?我有一個錯誤發佈您的logcat – CocoNess
您使用此代碼的地方? –
@TanjaV在一分鐘內更新了 –