나는 방법 jsnrqstr.sendRequest(username, password, URL);
새로운 실행 가능한 스레드없이 HTTPPost를 사용하는 방법
loginButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
username = userNameBox.getText().toString();
password = passwordBox.getText().toString();
login_state = jsnrqstr.sendRequest(username, password, URL);
if(login_state.equals("ok")){
details.setUserName(username);
details.setPassword(password);
Toast.makeText(MainActivity.this, "User Verified", Toast.LENGTH_LONG).show();
passwordBox.setText("");
Intent myIntent = new Intent(MainActivity.this, DashBoardActivity.class);
MainActivity.this.startActivity(myIntent);
}else
Toast.makeText(MainActivity.this, "Login Unsuccessful", Toast.LENGTH_LONG).show();
passwordBox.setText("");
}
});
이 방법은 수신에 대해 다음 코드를 사용하여 촬영 한 사용자 이름과 암호를 사용하여 응용 프로그램에 로그인 내 MainAcitivity
내부에 다음과 같은 코드가 있습니다 JSON 객체로 응답하는 erlang 서버의 응답과 이로부터 login_state를받을 수 있습니다. 에 대한 코드 jsnrqstr.sendRequest(username, password, URL);
public class JSONRequester {
String state;
public String sendRequest(final String userName, final String password, final String URL){
new Thread(new Runnable() {
public void run() {
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpResponse response;
JSONObject json = new JSONObject();
try{
HttpPost post = new HttpPost(URL);
post.setHeader("Content-type", "application/json");
json.put("username", userName);
json.put("password", password);
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
/*Checking response */
if(response!=null){
String temp = EntityUtils.toString(response.getEntity()); //Get the data in the entity
//Log.v("response", temp);
JsonObject o = new JsonParser().parse(temp).getAsJsonObject();
Log.v("response", o.get("state").toString());
state = o.get("state").toString();
}}
catch(Exception e){
e.printStackTrace();
//createDialog("Error", "Cannot Estabilish Connection");
}
}
}).start();
return state;
}
그러나 나는 내가 제대로 위의 방법에서 login_state
값을 반환하지 않은 것을 의미
if(login_state.equals("ok")){
라인 @널 포인터 예외 다음 얻는다. 나는 이것이 새로운 실행 가능한 쓰레드가 값을 반환 할 때가 너무 늦었다 고 생각하기 때문이다. 이 문제를 해결하기 위해 무엇을 할 수 있습니까?
감사합니다. 나는 일했다. – ssrp