Android Http响应不工作
我有一个注册部分,其中用户名和密码被发送到使用rest服务进行解析。这很好。在登录部分,我想检查用户名和密码是否在解析中注册,因此我接受http响应,但代码中有一些错误。由于某种原因,反应不会回来 我的登录成功完整方法如下:Android Http响应不工作,android,rest,web,parse-platform,Android,Rest,Web,Parse Platform,我有一个注册部分,其中用户名和密码被发送到使用rest服务进行解析。这很好。在登录部分,我想检查用户名和密码是否在解析中注册,因此我接受http响应,但代码中有一些错误。由于某种原因,反应不会回来 我的登录成功完整方法如下: private boolean loginSuccesfull() throws ClientProtocolException, IOException { // TODO Auto-generated method stub InputS
private boolean loginSuccesfull() throws ClientProtocolException, IOException {
// TODO Auto-generated method stub
InputStream result = null;
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet("https://api.parse.com/1/classes/Login?where={"+"\"UserName\""+":"+"\"abc\""+"}");
HttpResponse response = httpClient.execute(get);
if (response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(response.getEntity());
result = bufferedHttpEntity.getContent();
return true;
}else {
// insert error handling
return false;
}
}
解析服务器有一个内置的登录表,我们可以通过在该表中添加用户名和密码来使用该表,并按照该表检查登录是否成功
ParseUser parseUser = new ParseUser();
parseUser.logInInBackground( mEtUserName.getText().toString()
.trim()
,mEtPswd.getText().toString(),new LogInCallback() {
@Override
public void done(ParseUser arg0, com.parse.ParseException arg1) {
if(arg1==null ){
startActivity(new Intent(LoginActivity.this,CaptureActivity.class));
}
else{
arg1.printStackTrace();
Toast.makeText(LoginActivity.this,"UserName or Password is Incorrect", 0).show();
}
}
});
为此,我们必须首先使用以下代码在解析中注册用户名和密码
HttpPost request = new HttpPost(SERVICE_URI);
request.addHeader("X-Parse-Application-Id",
"e7ynC0ZB1ts5EQVpghywywoUUCNafT4XZnoJO3j5");
request.addHeader("X-Parse-REST-API-Key",
"w9iInpo0kPmzibzjyCkhyjO199tc5V0vqtafL2yW");
request.addHeader("Content-Type", "application/json");
// Build JSON string
JSONStringer TestApp = null;
try {
TestApp = new JSONStringer().object().key("username")
.value(mStrUname).key("password").value(mStrPswd).key("FullName").value(mStrFullName)
.endObject();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringEntity entity = null;
try {
entity = new StringEntity(TestApp.toString());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("****Parameter Input****", "Testing:" + TestApp);
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = null;
try {
response = httpClient.execute(request);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("WebInvoke", "Saving: " + response.getStatusLine().toString());
// Get the status of web service
BufferedReader rd = null;
try {
rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// print status in log
String line = "";
try {
while ((line = rd.readLine()) != null) {
Log.d("****Status Line***", "Webservice: " + line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
logcat中是否有错误?url中是否有任何内容需要使用UrlEncode()?是否使用解析库?如果不是,我建议你那样做。与手动发出http请求相比,他们的jar文件为您提供了一个更好的接口来与他们的API交互。@SiddharthVyas-ya我在我的query中遇到了非法参数异常错误,但query正在parse@jyomin所有这些都包含在解析文档中。另外,请查看快速入门指南。感谢各位的回答,我用上面的方法完成了。
HttpPost request = new HttpPost(SERVICE_URI);
request.addHeader("X-Parse-Application-Id",
"e7ynC0ZB1ts5EQVpghywywoUUCNafT4XZnoJO3j5");
request.addHeader("X-Parse-REST-API-Key",
"w9iInpo0kPmzibzjyCkhyjO199tc5V0vqtafL2yW");
request.addHeader("Content-Type", "application/json");
// Build JSON string
JSONStringer TestApp = null;
try {
TestApp = new JSONStringer().object().key("username")
.value(mStrUname).key("password").value(mStrPswd).key("FullName").value(mStrFullName)
.endObject();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringEntity entity = null;
try {
entity = new StringEntity(TestApp.toString());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("****Parameter Input****", "Testing:" + TestApp);
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = null;
try {
response = httpClient.execute(request);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("WebInvoke", "Saving: " + response.getStatusLine().toString());
// Get the status of web service
BufferedReader rd = null;
try {
rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// print status in log
String line = "";
try {
while ((line = rd.readLine()) != null) {
Log.d("****Status Line***", "Webservice: " + line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}