2013-02-19 46 views
0

以下JSON將被接收,但通過http://localhost/getData.php而是拋出異常從JSON獲取數據和org.json.JSONException拋出

{"username":"not found","password":null} 

登錄我收到

02-19 17:31:54.745: E/JSON Parser(5277): Error parsing data org.json.JSONException: End of input at character 0 of 
02-19 17:31:59.185: E/JSON Parse(5277): ERROR 

下面的代碼是方法在那裏拋出異常

@Override 
    protected String doInBackground(String... url){   

     try{ 
      String resultText = ""; 
      EditText edit = (EditText)findViewById(R.id.text_box); 
      id = edit.getText().toString(); 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("id",id)); 

      JSONObject json = jsonParser.HttpRequest("http://localhost/getData.php", params); 

      resultText = json.getString("username"); 



     }catch(Exception e){ 
      e.printStackTrace(); 
      Log.e("JSON Parse","ERROR"); 
     } 

     return ""; 
    } 

public class SimpleJsonParser {

public SimpleJsonParser(){ 

} 


public JSONObject HttpRequest(String url, List<NameValuePair> params){ 
    InputStream input = null; 
    JSONObject jObj = null; 
    String json = ""; 
    String line; 

    StringBuilder builder = new StringBuilder(); 
    HttpClient client = new DefaultHttpClient(); 
    paramsString = URLEncodedUtils.format(params,"utf-8"); 
    url += "?" + paramsString; 
    HttpGet httpGet = new HttpGet(url); 


    try{ 


     HttpResponse response = client.execute(httpGet); 
     StatusLine statusLine = response.getStatusLine(); 

     if(statusLine.getStatusCode() == HttpStatus.SC_OK){ 
      HttpEntity entity = response.getEntity(); 
      input = entity.getContent(); 
      BufferedReader reader = 
        new BufferedReader(new InputStreamReader(input)); 


      while((line = reader.readLine())!=null){ 
       builder.append(line); 
      } 
      json = builder.toString(); 

     } else { 
      Log.e("JSON Parser","Failed to download file"); 
     } 
    }catch(ClientProtocolException e){ 
     e.printStackTrace(); 
    }catch(IOException e){ 
     e.printStackTrace(); 
    } 

    try { 
     jObj = new JSONObject(json); 
     input.close(); 
    } catch (Exception e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    return jObj; 
} 

}

什麼錯我的代碼?我提供的代碼是發生異常的地方

+2

是的,訪問本地主機,這是Android設備,而不是你的服務器。 – njzk2 2013-02-19 17:45:11

回答

0

裏面simpleJarsonParser類,我將以下代碼

HttpClient httpClient = new DefaultHttpClient(); 

DefaultHttpClient httpClient = new DefaultHttpClient(); 

和它的作品。當然,我使用xampp和android模擬器替換localhost到10.0.2.2。

2

從你所提到的看來,你是通過模擬器來做到這一點的。可能無法訪問http://localhost//。您可以使用,回送地址或IP地址嘗試訪問。

+0

好點。在過去,我對Android模擬器有太多問題,我發現它甚至不值得承受壓力。 – 2013-02-19 17:54:08

+0

如果你閱讀他的代碼,他會得到一個HTTP響應,這是一個200;問題是它沒有內容。 – 2013-02-19 17:59:45

2

你從來沒有驗證你實際上從HttpEntity返回的流讀取後什麼都收到了什麼。這個例外是告訴你在字符0(第一個字符)處沒有輸入 - 你沒有任何可解析的東西;它是一個空字符串。您的StringBuilder沒有任何附加內容。

public static void main(String[] args) throws JSONException 
{ 
    String json = "{\"username\":\"not found\",\"password\":null}"; 
    JSONObject jObj = new JSONObject(json); 
} 

拋出此類異常。通常編寫一個小的測試用例會爲您指出正確的方向。

正如其他人所說,這可能是因爲您使用的是錯誤的URL或可能是您的參數?既然你回來200 OK你很明顯連接到一個網絡服務器並得到一個響應......我會檢查,看看這個PHP是否按照你的預期工作。

+0

我在我的代碼中有2個錯誤,其中一個是別人說我的URL錯誤,另一個是沒有從我的PHP文件返回。我想我必須打開一個新的線程,詢問我的PHP文件。 – edisonthk 2013-02-20 07:32:15