2014-09-04 14 views
0

我是使用JSON的新手,我試圖在服務器上傳一些文本並顯示它。目前,我正在做簡單的登錄和發表評論。我得到錯誤。有人請幫忙。轉換結果的Android錯誤java.lang.NullPointerException

Log Cat message: 
09-04 10:35:34.820: E/Buffer Error(12622): Error converting result java.lang.NullPointerException 
09-04 10:35:34.820: E/JSON Parser(12622): Error parsing data org.json.JSONException: End of input at character 0 of 
09-04 10:35:34.820: W/dalvikvm(12622): threadid=11: thread exiting with uncaught exception (group=0x40ccb2a0) 
09-04 10:35:34.840: E/AndroidRuntime(12622): FATAL EXCEPTION: AsyncTask #1 
09-04 10:35:34.840: E/AndroidRuntime(12622): java.lang.RuntimeException: An error occured while executing doInBackground() 
09-04 10:35:34.840: E/AndroidRuntime(12622): at android.os.AsyncTask$3.done(AsyncTask.java:299) 
09-04 10:35:34.840: E/AndroidRuntime(12622): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
09-04 10:35:34.840: E/AndroidRuntime(12622): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
09-04 10:35:34.840: E/AndroidRuntime(12622): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
09-04 10:35:34.840: E/AndroidRuntime(12622): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
09-04 10:35:34.840: E/AndroidRuntime(12622): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
09-04 10:35:34.840: E/AndroidRuntime(12622): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
09-04 10:35:34.840: E/AndroidRuntime(12622): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
09-04 10:35:34.840: E/AndroidRuntime(12622): at java.lang.Thread.run(Thread.java:856) 

JSONParser.java

公共類JSONParser {

static InputStream is = null; 
static JSONObject jObj = null; 
static String json = ""; 

// constructor 
public JSONParser() { 

} 

public JSONObject getJSONFromUrl(final String url) { 

    // Making HTTP request 
    try { 
     // Construct the client and the HTTP request. 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 

     // Execute the POST request and store the response locally. 
     HttpResponse httpResponse = httpClient.execute(httpPost); 
     // Extract data from the response. 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     // Open an inputStream with the data content. 
     is = httpEntity.getContent(); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     // Create a BufferedReader to parse through the inputStream. 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     // Declare a string builder to help with the parsing. 
     StringBuilder sb = new StringBuilder(); 
     // Declare a string to store the JSON object data in string form. 
     String line = null; 

     // Build the string until null. 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 

     // Close the input stream. 
     is.close(); 
     // Convert the string builder data to an actual string. 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    // Try to 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 the JSON Object. 
    return jObj; 

} 

// function get json from url 
// by making HTTP POST or GET mehtod 
public JSONObject makeHttpRequest(String url, String method, 
     List<NameValuePair> params) { 

    // Making HTTP request 
    try { 

     // check for request method 
     if (method == "POST") { 
      // request method is POST 
      // 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(); 

     } else if (method == "GET") { 
      // request method is GET 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      String paramString = URLEncodedUtils.format(params, "utf-8"); 
      url += "?" + paramString; 
      HttpGet httpGet = new HttpGet(url); 

      HttpResponse httpResponse = httpClient.execute(httpGet); 
      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(); 
    } 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; 

} 

}

Login.java

類AttemptLogin延伸的AsyncTask {

@Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(Login.this); 
     pDialog.setMessage("Attempting login..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    @Override 
    protected String doInBackground(String... args) { 
     // TODO Auto-generated method stub 
     // Check for success tag 
     int success; 
     String username = user.getText().toString(); 
     String password = pass.getText().toString(); 
     try { 
      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("username", username)); 
      params.add(new BasicNameValuePair("password", password)); 

      Log.d("request!", "starting"); 
      // getting product details by making HTTP request 
      JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", 
        params); 

      // check your log for json response 
      Log.d("Login attempt", json.toString()); 

      // json success tag 
      success = json.getInt(TAG_SUCCESS); 
      if (success == 1) { 
       Log.d("Login Successful!", json.toString()); 
       // save user data 
       SharedPreferences sp = PreferenceManager 
         .getDefaultSharedPreferences(Login.this); 
       Editor edit = sp.edit(); 
       edit.putString("username", username); 
       edit.commit(); 

       Intent i = new Intent(Login.this, ReadComments.class); 
       finish(); 
       startActivity(i); 
       return json.getString(TAG_MESSAGE); 
      } else { 
       Log.d("Login Failure!", json.getString(TAG_MESSAGE)); 
       return json.getString(TAG_MESSAGE); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return null; 

回答

2

錯誤解析數據org.json.JSONException:人品的 輸入完...

這意味着你是你不是得到任何得到任何JSON。

你需要做的是找出爲什麼你的應用程序沒有收到數據。

可能的原因:

  • 網絡相關的問題
  • 您的應用程序缺少許可
  • 服務器沒有發回的響應。
+0

你是對的...... – 2014-09-04 03:28:42

相關問題