80,351
社区成员
发帖
与我相关
我的任务
分享
public class NetworkingActivity extends Activity {
private InputStream OpenHttpConnection(String urlString) throws IOException
{
InputStream in=null;
int response=-1;
URL url=new URL(urlString);
URLConnection conn=url.openConnection();
if(!(conn instanceof HttpURLConnection))
throw new IOException("未连接");
try
{
HttpURLConnection httpConn=(HttpURLConnection)conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response=httpConn.getResponseCode();
if(response==HttpURLConnection.HTTP_OK)
{
in=httpConn.getInputStream();
}
}
catch(Exception ex)
{
Log.d("Networking",ex.getLocalizedMessage());
throw new IOException("连接错误");
}
return in;
}
private String DownloadText(String URL)
{
int BUFFER_SIZE=2000;
InputStream in=null;
try
{
in=OpenHttpConnection(URL);
}
catch(IOException e)
{
Log.d("Networking",e.getLocalizedMessage());
return "";
}
InputStreamReader isr=new InputStreamReader(in);
int charRead;
String str="";
char[] inputBuffer=new char[BUFFER_SIZE];
try
{
while((charRead=isr.read(inputBuffer))>0)
{
String readString=String.copyValueOf(inputBuffer, 0, charRead);
str+=readString;
inputBuffer=new char[BUFFER_SIZE];
}
in.close();
}
catch(IOException e)
{
Log.d("Networking",e.getLocalizedMessage());
return "";
}
return str;
}
private class DownloadTextTask extends AsyncTask<String,Void,String>
{
protected String doInBackground(String... urls)
{
return DownloadText(urls[0]);
}
@Override
protected void onPostExecute(String result)
{
Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_networking);
new DownloadTextTask().execute("http://m.baidu.com");
} }
public class MainActivity extends Activity implements OnClickListener {
private Button sendRequest;
private TextView responseText;
private Handler handler=new Handler(){
public void handleMessage(Message msg)
{
switch(msg.what)
{
case 0:
String response=(String)msg.obj;
responseText.setText(response);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendRequest=(Button)findViewById(R.id.send_request);
responseText=(TextView)findViewById(R.id.response_text);
sendRequest.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if(v.getId()==R.id.send_request)
{
sendRequestWithHttpURLConnection();
}
}
private void sendRequestWithHttpURLConnection()
{
new Thread(new Runnable()
{
@Override
public void run()
{
HttpURLConnection connection=null;
try
{
URL url=new URL("http://m.baidu.com");
connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream in=connection.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(in));
StringBuilder response=new StringBuilder();
String line;
while((line=reader.readLine())!=null)
{
response.append(line);
}
Message message=new Message();
message.what=0;
message.obj=response.toString();
handler.sendMessage(message);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(connection!=null)
{
connection.disconnect();
}
}
}
}).start();
} }