80,351
社区成员
发帖
与我相关
我的任务
分享
Activity
private Button mButton1,mButton2;
private TextView mTextView1;
private JoyHttp j;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*透过findViewById建构子建立TextView与Button对象*/
mButton1 =(Button) findViewById(R.id.myButton1);
mButton2 =(Button) findViewById(R.id.myButton2);
mTextView1 = (TextView) findViewById(R.id.myTextView1);
/*设定OnClickListener来聆听OnClick事件*/
mButton1.setOnClickListener(new Button.OnClickListener()
{
/*重写onClick事件*/
@Override
public void onClick(View v)
{
/*声明网址字符串*/
String uriAPI = "http://localhost:8080";
String message = "123";
// post(uriAPI);
mTextView1.setText(j.post(uriAPI,message));
}
});
Post
public String post(String uriAPI, String message)
{
String fal = "fal";
HttpPost httpRequest = new HttpPost(uriAPI);
try
{
/* 发出HTTP request */
httpRequest.setEntity(new StringEntity(message));
// httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
/* 取得HTTP response */
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
/* 若状态码为200 ok */
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
/* 取出响应字符串 */
String strResult = EntityUtils.toString(httpResponse.getEntity());
// mTextView1.setText(strResult);
return strResult;
}
// else
// {
// mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString());
// }
} catch (ClientProtocolException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
}
return fal;
}