现在情况如下:
IDE:Eclipse j2ee
Android Project:NetClient
src:com.android.client.NetworkClientActivitypackage com.android.client;import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;public class NetworkClientActivity extends Activity {
/** Called when the activity is first created. */
private Button button1 = null;
private EditText editText1 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button)findViewById(R.id.button1);
editText1 = (EditText)findViewById(R.id.editText1);
button1.setOnClickListener(new Button1Listener());
}
class Button1Listener implements OnClickListener{
@Override
public void onClick(View v) {
String urlString = "http://10.0.2.2:8888/HTTPServlet/com/android/server/LoginServlet";
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
InputStream in = conn.getInputStream();
byte[] b = new byte[in.available()];
in.read(b);
String msg = new String(b);
editText1.setText(msg);
in.close();
}
conn.disconnect();
} catch (MalformedURLException e) {
editText1.setText(e.toString());
} catch(IOException e){
editText1.setText(e.toString());
}
}
}
}
IDE:Eclipse j2ee
Android Project:NetClient
src:com.android.client.NetworkClientActivitypackage com.android.client;import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;public class NetworkClientActivity extends Activity {
/** Called when the activity is first created. */
private Button button1 = null;
private EditText editText1 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button)findViewById(R.id.button1);
editText1 = (EditText)findViewById(R.id.editText1);
button1.setOnClickListener(new Button1Listener());
}
class Button1Listener implements OnClickListener{
@Override
public void onClick(View v) {
String urlString = "http://10.0.2.2:8888/HTTPServlet/com/android/server/LoginServlet";
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
InputStream in = conn.getInputStream();
byte[] b = new byte[in.available()];
in.read(b);
String msg = new String(b);
editText1.setText(msg);
in.close();
}
conn.disconnect();
} catch (MalformedURLException e) {
editText1.setText(e.toString());
} catch(IOException e){
editText1.setText(e.toString());
}
}
}
}
解决方案 »
- android 修改preference中view属性(title,summary字体颜色等)
- Android有没类似IPhone中Game Center的apk,要求是外文的,谢谢!
- gridview的item可否在屏幕上移动
- apache的HttpClient 执行总是异常,求助啊!
- 求助关于发邮件authenticatorException
- android 从手机相册中取出图片后,随意摆放在屏幕上,如何实现
- 在Cygwin中添加该代码的作用是什么?(ndk是android ndk)
- Post请求异常,求教
- 请问为什么getMainLooper和主线程通信失败
- AccessibilityService操作流程完成后如何跳转到自己的APP页面?
- android开发中背景图片不清楚
- 求助如何能找到英文的android技术文档?
src:com.android.server.LoginServletimport java.io.IOException;
import java.io.PrintWriter;import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response){
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out;
try {
out = response.getWriter();
out.print("hello,android");
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response){
doGet(request,response);
}
}