2013-02-28 84 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