POST请求总是在android

时间:2016-01-20 17:47:03

标签: android html json post

我正在构建一个简单的Android应用程序,我正在创建一个登录页面并实现一个简单的POST Web API。以前它工作正常但突然停止工作,现在它总是返回我的HTML代码。以下是我的代码,

 try {

         List<NameValuePair> li = new ArrayList<NameValuePair>();
            li.add(new BasicNameValuePair("username", "ma@dhdh.com"));
            li.add(new BasicNameValuePair("password", "123456dndjsj789"));
            li.add(new BasicNameValuePair("key", "ZATXMhjHartjDTrH"));
        HttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

            HttpPost httppost = new HttpPost("http://www.deal.com.lb/merchant_ws/login_ws.php");

            httppost.setHeader("Content-Type", "text/html");
            httppost.setHeader("Accept","application/json");

            httppost.setEntity(new UrlEncodedFormEntity(li));

            httpResponse = httpClient.execute(httppost);

        httpEntity = httpResponse.getEntity();
        String response = EntityUtils.toString(httpEntity);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 

请告诉我我写错了哪些代码。此服务也可用作GET请求。你可以在浏览器中试试。在浏览器中它正常工作,但在Android代码中它不是。

1 个答案:

答案 0 :(得分:0)

您是否设置了header('Content-Type: application/json'); 在你的PHP?你有一个跟踪Java代码的日志吗?