我正在尝试在Eclipse EE中编写一个servlet,它向android app.Android app中的client.Client发送一条消息,它有一个从servlet获取消息的Button和一个显示该消息的TextView。但是TextView什么也没显示。 这是我的代码:
-servlet:
package nguyen.lan;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloServlet
*/
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out=response.getWriter();
response.setContentType("text/plain");
out.println("nguyen ngoc lan");
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
-Android app:
package nguyen.lan;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class DemoAndroidActivity extends Activity {
/** Called when the activity is first created. */
private TextView myTextView;
Button mButton;
String url="http://192.168.1.35:10536/myServlet/HelloServlet";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTextView=(TextView) findViewById(R.id.mTextView);
mButton=(Button) findViewById(R.id.button1);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getString(v);
}
});
}
public void getString(View v) {
InputStream responseStream;
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(url);
try {
responseStream=httpClient.execute(httpGet).getEntity().getContent();
BufferedReader reader=new BufferedReader(new InputStreamReader(responseStream));
StringBuilder sb=new StringBuilder();
while(reader.readLine()!=null)
{
sb.append(reader.readLine());
}
String mString=sb.toString();
myTextView.setText(mString);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- 我也在AndroidManifest.xml中使用权限INTERNET