RESTful Web服务json响应在java中返回null

时间:2016-08-16 10:29:52

标签: java android json

我是使用Java的Android和Web服务编程的新手,我一直在将我的Android应用程序连接到我的java Web服务时遇到问题。 我已经在tomcat上部署了Web服务,并且它在我的手机浏览器上连接,但每次我尝试从我的Android应用程序连接时,HttpClient都会返回null。

这是我的源代码。

public class ServiceHandler {

static String response = null;
public final static int GET = 1;
public final static int POST = 2;

public ServiceHandler() {

}

/**
 * Making service call
 * @url - url to make request
 * @method - http request method
 * */
public String makeServiceCall(String url, int method) {
    return this.makeServiceCall(url, method, null);
}

/**
 * Making service call
 * @url - url to make request
 * @method - http request method
 * @params - http request params
 * */
public String makeServiceCall(String url, int method, List<NameValuePair> params) {
    try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            // adding post params
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }

            httpResponse = httpClient.execute(httpPost);

        } else if (method == GET) {
            // appending params to url
            if (params != null) {
                String paramString = URLEncodedUtils
                        .format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);

            httpResponse = httpClient.execute(httpGet);

        }
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);

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

    return response;

}

}

private class Authenticate extends AsyncTask<Void, Void, Void>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();


        String url = "http://192.168.34.1:8080/SchoolWebService/rest/User/"+username+","+password;

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject c = new JSONObject(jsonStr);

                String uname = c.getString("username");
                String pword = c.getString("password");
                snackbar.setText("Authenticated").show();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            snackbar.setText("Authentication Failed!").show();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
}

Log.d(....);语句总是返回null。

1 个答案:

答案 0 :(得分:0)

谢谢你们,但我终于找到了解决方案。正如@foobar评论的那样,makeServiceCall方法抛出了一个我没有看到的异常,因此我取出了printStackTrace()并将其替换为Log.e(TAG,Log.getStackTraceString(e));然后我发现我的手机无法通过网络连接到我的电脑。我收到连接被拒绝错误消息。所以我做了以下事情。

  1. 打开手机上的USB网络共享。
  2. 从命令提示符输入ipconfig并从我的USB连接192.168.42.xxx获取我的IPv4地址。
  3. 我用此替换了我的网址中的IP地址。
  4. 我在我的端口号上添加了入站和出站规则到我的防火墙,所有内容都开始工作
  5. 谢谢你们