2013-02-28 83 views
1

我有一個android的登錄應用程序,它使用JSON將數據從數據庫解析到應用程序。接受HTTP請求通過標籤識別的PHP API,要麼是「登錄」或「註冊」,像這樣:解析JSONobject時出錯

if (isset($_POST['tag']) && $_POST['tag'] != '') { 
    "Do stuf 
    } else { 
     echo "access denied"; 

的應用程序已經工作正常,但現在我只是得到響應「訪問拒絕」。

public JSONObject loginUser(String email, String password){ 
    // Building Parameters 
    List<NameValuePair> params = new ArrayList<NameValuePair>(); 
    params.add(new BasicNameValuePair("tag", "login")); 
    params.add(new BasicNameValuePair("email", email)); 
    params.add(new BasicNameValuePair("password", password)); 
    JSONObject json = jsonParser.getJSONFromUrl(loginURL, params); 
    // return json 
    // Log.e("JSON", json.toString()); 
    return json; 
} 

這是發送請求的JSON對象,並且我懷疑它沒有正確發送標記。有沒有人知道發生了什麼?

更新:新增JSONparser.class

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, "UTF-8"), 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; 

} 

}

+2

爲什麼你不只是調試自己陷入更多的一些信息僅通過在你的PHP腳本中看到$ _POST數組的實際內容 – 2013-02-28 14:00:23

+0

我回應了$ _POST [「tag」],它返回Null,所以由於某種原因JSONparser沒有發送數據。 – 2013-02-28 14:09:11

+0

你可以用strlen($ _ POST ['tag'])> 0替換所有的東西(isset($ _ POST ['tag'])&& $ _POST ['tag']!='') jsonParser.getJSONFromURL使用$ _GET而不是$ _POST – Dave 2013-02-28 14:32:53

回答

0

其實在看它,它看起來像你使用JSON查詢錯誤反正unles有更多的代碼,你不向我們展示。你錯過了jsonParser類

public class JSONParser {

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

// constructor 
public JSONParser() { 

} 

public JSONObject getJSONFromUrl(String url) { 

    // Making HTTP request 
    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 

     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(); 
    } 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; 

} } 

創建瀏覽此網站了解更多信息http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

+0

其實這就是我得到這個代碼。它一直在完美工作,但是我幾個星期沒有碰到我的項目,現在它發送$ _POST數組值爲空。我應該把JSONParser.class放在裏面以便查看它嗎? – 2013-03-01 13:49:59

+0

它肯定會有助於看到你的完整代碼是啊。但是,如果它的工作完美並且突然停止了機會,並且您沒有更改入站請求的文件或處理文件,則問題出在服務器配置的某處,而不是您的代碼。它或者導致與最終服務器不同的響應或者本地服務器沒有正確處理請求 – Dave 2013-03-01 13:52:11

+0

我添加了JSONParser.class。我試圖回顯出$ _POST,但它是空的。它可能以$ _GET的形式發送數據嗎? – 2013-03-01 14:34:36