java 中的 httpResponse returns 为空
httpResponse returns null in java
我正在尝试从我的服务器端获取对我的应用程序的响应,并且在我的 HTTP 响应中得到了 null
。当我在浏览器中粘贴 URL 时,我得到了想要的响应。
URL:“http://localhost:8080/TBookServerSide/LoginServlet?u=admin&p=123”
响应:
{ "info" : "success" }
就像我得到一个 JSON 对象返回,在我的信息参数中包含成功一词
但是,当我尝试使用 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;
}
}
}
JSONParser.java
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;
}
}
LogCat:
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
我不知道为什么它没有按预期工作,非常感谢你们的帮助。
http://localhost:8080/
只能由您的计算机识别,因为它托管在该计算机上。此处,localhost
表示您配置服务器的计算机,因此您的浏览器能够 locate/find 它。
当您在移动设备上调用相同时,localhost
表示您的移动设备而不是您的计算机。这就是它不起作用的原因。
您仍然可以使用本地服务器进行开发。尝试连接 this.
如果你是 运行 windows 查找计算机的 ip : 按 windows 键 + R 然后键入 CMD 然后键入 ipconfig 并按 enter
之后你可以在IPv4-Address前面看到你的IP地址
键入它而不是本地主机
你知道localhost的意思吗?它是你个人电脑的本地ip,所以你可以在你的浏览器中成功。
如果你只是想在你的 app.You 上做一个测试,应该在你的电脑上打开一个热点,然后让你的 android phone 连接上它。
或者像这样将本地 url 更改为外部 url:http://wthrcdn.etouch.cn/weather_mini?citykey=101010100
我正在尝试从我的服务器端获取对我的应用程序的响应,并且在我的 HTTP 响应中得到了 null
。当我在浏览器中粘贴 URL 时,我得到了想要的响应。
URL:“http://localhost:8080/TBookServerSide/LoginServlet?u=admin&p=123”
响应:
{ "info" : "success" }
就像我得到一个 JSON 对象返回,在我的信息参数中包含成功一词
但是,当我尝试使用 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;
}
}
}
JSONParser.java
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;
}
}
LogCat:
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
我不知道为什么它没有按预期工作,非常感谢你们的帮助。
http://localhost:8080/
只能由您的计算机识别,因为它托管在该计算机上。此处,localhost
表示您配置服务器的计算机,因此您的浏览器能够 locate/find 它。
当您在移动设备上调用相同时,localhost
表示您的移动设备而不是您的计算机。这就是它不起作用的原因。
您仍然可以使用本地服务器进行开发。尝试连接 this.
如果你是 运行 windows 查找计算机的 ip : 按 windows 键 + R 然后键入 CMD 然后键入 ipconfig 并按 enter
之后你可以在IPv4-Address前面看到你的IP地址
键入它而不是本地主机
你知道localhost的意思吗?它是你个人电脑的本地ip,所以你可以在你的浏览器中成功。
如果你只是想在你的 app.You 上做一个测试,应该在你的电脑上打开一个热点,然后让你的 android phone 连接上它。
或者像这样将本地 url 更改为外部 url:http://wthrcdn.etouch.cn/weather_mini?citykey=101010100