httpResponse在java中返回null

httpResponse在java中返回null,java,android,networking,httpresponse,Java,Android,Networking,Httpresponse,我试图从我的服务器端获取对我的应用程序的响应,我在HTTP响应中得到null。当我在浏览器中粘贴URL时,我得到了想要的响应 网址:“ 答复如下: { "info" : "success" } 就像我得到一个JSON对象,在我的info参数中有success这个词 但是,当我尝试使用android应用程序时,我得到的却是null 我需要填写登录表单(用户和密码),然后按下登录按钮,它才能工作 这是我的密码: LoginFragment.java: public class LoginFragm

我试图从我的服务器端获取对我的应用程序的响应,我在HTTP响应中得到
null
。当我在浏览器中粘贴URL时,我得到了想要的响应

网址:“

答复如下:

{ "info" : "success" }
就像我得到一个JSON对象,在我的info参数中有success这个词 但是,当我尝试使用android应用程序时,我得到的却是
null

我需要填写登录表单(用户和密码),然后按下登录按钮,它才能工作

这是我的密码:

LoginFragment.java:

public class LoginFragment extends Fragment {
    public LoginFragment() {
        // Required empty public constructor
    }

    EditText firstNameText;
    EditText lastNameText;
    EditText userNameText;
    EditText passwordText;
    EditText emailText;

    Button loginButton;
    Button signUpPageButton;

    // Creating JSON Parser object
    com.example.liran.tbookandroid.LoginPage.JSONParser jParser = new com.example.liran.tbookandroid.LoginPage.JSONParser();


    private static String url_login = "http://localhost:8080/TBookServerSide/LoginServlet";
    //JSONArray incoming_msg = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_login, container, false);

        (...)

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                User newUser = new User();
                String userName = userNameText.getText().toString();
                String password = passwordText.getText().toString();

                newUser.setUserName(userName);
                newUser.setPassword(password);

                Log.d("Press Button:","Login Button has been pressed");
                new LoginWithServlet(newUser).execute();
            }
        });

        return root;
    }

    private class LoginWithServlet extends AsyncTask<String, String, String> {
        JSONObject json;
        User user;
        boolean isUserExist = false;

        public LoginWithServlet(User user) {
            this.user = user;
        }

        @Override
        protected String doInBackground(String... params2) {
            // Getting username and password from user input
            String username = user.getUserName();
            String pass = user.getPassword();

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("u", username));
            params.add(new BasicNameValuePair("p", pass));
            json = jParser.makeHttpRequest(url_login, "GET", params);
            String s = null;

            try {
                if (json != null) {
                    s = json.getString("info");
                    Log.d("Msg", json.getString("info"));
                    if (s.equals("success")) {
                        // SUCCEED LOGIN !!!!
                        Log.d("Login Servlet: "," Login Succeed");
                        ((MainActivity) getActivity()).openMainPageFragment();
                        ((MainActivity) getActivity()).showBarButtons();
                    } else {
                        // FAILED LOGIN
                        Log.d("Login Servlet: "," Login failed");
                    }
                } else {
                    Log.e("JSon:"," is NULL");
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }
    }

}

我不知道为什么它不能按预期工作,我非常感谢你们的帮助。

http://localhost:8080/
仅由您的计算机标识,因为它位于该计算机上。这里,
localhost
表示您配置服务器的机器,因此您的浏览器能够找到它

当您在移动设备上调用相同的时,
localhost
表示您的移动设备,而不是您的计算机。这就是它不起作用的原因


您仍然可以使用本地服务器进行开发。尝试像这样连接。

http://localhost:8080/
仅由您的计算机标识,因为它位于该计算机上。这里,
localhost
表示您配置服务器的机器,因此您的浏览器能够找到它

当您在移动设备上调用相同的时,
localhost
表示您的移动设备,而不是您的计算机。这就是它不起作用的原因


您仍然可以使用本地服务器进行开发。尝试像这样连接。

如果您正在运行windows,请查找计算机的ip:按windows键+R,然后键入CMD,然后键入ipconfig,然后按enter

之后,您可以在IPv4地址前面看到您的IP地址


如果您正在运行windows,请键入它而不是localhost查找计算机的ip:按windows键+R,然后键入CMD,然后键入ipconfig并按enter

之后,您可以在IPv4地址前面看到您的IP地址


键入它而不是localhost

您知道localhost的含义吗?它是您个人计算机的本地ip,因此您可以在浏览器中成功地执行它

如果你只想在你的应用程序中做一个测试,你应该在你的电脑上打开一个热点,然后让你的android手机连接到它


或者将您的本地url更改为外部url,如下所示:

您知道localhost的含义吗?它是您个人计算机的本地ip,因此您可以在浏览器中成功实现

如果你只想在你的应用程序中做一个测试,你应该在你的电脑上打开一个热点,然后让你的android手机连接到它



或者将您的本地url更改为外部url,如下所示:

logcat中是否存在任何异常/错误?您是否在emulator或设备中运行?
localhost
您的Android设备与运行网站的PC/任何设备的
localhost
不同。是的,我发布了logcat。im从emulator运行。不推荐使用DefaultHttpClient方法尝试HttpURLConnection访问网络请求。logcat中是否存在任何异常/错误?是否在emulator或设备中运行?Android设备的
localhost
与PC/运行网站的任何设备的
localhost
不同。是,我贴了日志。im从emulator运行。DefaultHttpClient方法不推荐尝试HttpURLConnection访问网络请求。我正在使用一个模拟器,所以技术上这是请求httpresponseif的计算机,那么,我应该写什么来代替本地主机呢?您应该将移动设备连接到与计算机连接的同一网络上,然后找到计算机的IP(本地网络提供的不是外部IP的IP)。你可以使用这个IP。我正在使用一个模拟器,所以从技术上来说,这是一台需要httpresponseif的计算机,那么我应该写什么来代替localhost?你应该将你的移动设备连接到与你的计算机连接的同一个网络上,然后找到计算机的IP(本地网络提供的不是外部IP的IP). 你可以使用这个IP。我已经从CMD命令中输入了内部IP,它工作得非常好。谢谢你,伙计!我已经从CMD命令中输入了内部ip,它工作得非常好。谢谢你,伙计!
public class JSONParser {
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    static InputStream iStream = null;
    static JSONArray jarray = null;
    //static String json = "";

    // constructor
    public JSONParser() {}

    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
        // Making HTTP request
        try {
            // check for request method
            if(method == "POST"){
                // request method is POST
                // 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();
            } else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }

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

    }
}
com.example.liran.tbookandroid E/Buffer Error: Error converting result java.lang.NullPointerException: lock == null
com.example.liran.tbookandroid E/JSON Parser: Error parsing data org.json.JSONException: End of input at character 0 of 
com.example.liran.tbookandroid E/JSon::  is NULL