我正在尝试使用WCF REST服务来传达我的Android应用程序,但是当我运行Android应用程序时,总会出现一条消息,说不幸的是,Android应用程序已经停止。
以下是我对该服务的调用,当点击执行事件LogarOnserver时,我的应用程序只有一个具有按钮的活动,如下所示。在服务返回响应后,我将其取出并放入TextView(_myPassWord)。
package com.arduino.ligthautomation;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class RegisterActivity extends Activity {
EditText _myPassWord ;
EditText _myLogin;
private static final String URL_STRING = "http://192.168.0.3:8080/lightautomationservice/1";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//mostrando tela de login
setContentView(R.layout.login);
_myPassWord = (EditText) findViewById(R.id.editTextPassword);
_myLogin = (EditText) findViewById(R.id.editTextUsuario);
}
public void logarOnServer (View v) throws IOException
{
try{
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet("http://192.168.0.3:8080/lightautomationservice/1");
ResponseHandler<String> handler = new BasicResponseHandler();
//you result will be String :
String result = httpclient.execute(request, handler);
_myLogin.setText(result);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}