我有一个注册部分,其中使用rest服务发送用户名和密码进行解析。这工作正常。在登录部分,我想检查用户名和密码是否在解析中注册,所以我采取了http响应,但我的代码中有一些错误。由于某种原因,回复没有回来。
我的loginSuccesfull方法如下:
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;
}
}
答案 0 :(得分:1)
to make a successful HTTP response and handle all exception , use the following Code:
HttpParams httpParams = new BasicHttpParams();
ConnManagerParams.setTimeout(httpParams, 10000);
HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
HttpConnectionParams.setSoTimeout(httpParams, 30000);
HttpClient httpClient = new DefaultHttpClient(httpParams);
HttpPost httpPost = new HttpPost(BASE_URL);
httpPost.setEntity(new ByteArrayEntity(request.toString()
.getBytes()));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
System.out.println("UnsupportedEncodingException >>>>>>> "
+ e.getMessage());
} catch (ClientProtocolException e) {
e.printStackTrace();
System.out.println("ClientProtocolException >>>>>>> "
+ e.getMessage());
} catch (ConnectTimeoutException e) {
Log.e("CONN TIMEOUT", e.toString());
e.printStackTrace();
} catch (SocketTimeoutException e) {
Log.e("SOCK TIMEOUT", e.toString());
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
System.out.println("IOException >>>>>>> " + e.getMessage());
} catch (Exception e) {
Log.e("OTHER EXCEPTIONS", e.toString());
System.out.println("Exception >>>>>>> " + e.getMessage());
}
答案 1 :(得分:0)
解析服务器有一个内置的登录表,我们可以通过在该表中添加用户名和密码来使用此表,并按照此操作检查登录是否成功
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;
}