Android网络编程http派/申请服务

时间:2021-07-27 23:14:53
最近的研究Android网络编程知识,这里有一些想法,今晚学习。与您分享。

在实际的应用程序的开发非常需要时间appserver请求数据,那么app怎样发送请求呢?以下的代码就是当中的一种情况。使用HttpURLConnection向server发送请求的数据,然后处理server返回的数据。



以下的代码仅仅是client的一个简答測试代码。还须要在pc上安装一个 tomcatserver ,然后将demo中的jsp文件部署到tomcat上。详细的方法百度一下便知。

package com.lee.nethttp;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL; import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class MainActivity extends Activity { private EditText mEditText = null;
private Button mButton = null;
private TextView mTextView = null; private String mUrl = "http://192.168.0.109:8080/lee/index.jsp";//依据自己主机的ip设置 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mEditText = (EditText) findViewById(R.id.edit);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(mButtonClickListener);
mTextView = (TextView) findViewById(R.id.text); } private OnClickListener mButtonClickListener = new OnClickListener() { @Override
public void onClick(View v) {
String content = mEditText.getText().toString();
content = toBase64(content);
String[] params = { content };
new NetHttpAsyncTask().execute(params);
}
}; private class NetHttpAsyncTask extends AsyncTask<String, Void, String> { @Override
protected String doInBackground(String... params) {
String result = null; try {
URL url = new URL(mUrl);
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-type",
"application/x-www-form-urlencoded;charset=UTF-8");
connection.setConnectTimeout(3000);
connection.setReadTimeout(3000); PrintWriter writer = new PrintWriter(connection.getOutputStream());
String str = "content" + "=" + params[0];
writer.print(str);
writer.flush();
writer.close(); InputStream input = connection.getInputStream();
ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();
int ch;
while((ch = input.read()) != -1){
outputBuffer.write(ch);
} result = outputBuffer.toString();
} catch (IOException e) {
e.printStackTrace();
} return result;
} @Override
protected void onPostExecute(String result) {
mTextView.setText(result);
}
} private String toBase64(String content){
try {
content = Base64.encodeToString(content.getBytes("UTF-8"), Base64.DEFAULT);
Log.e("lee", "toBase64 content = " + content);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} return content;
}
}

Demo 下载地址

版权声明:本文博主原创文章,博客,未经同意不得转载。