android SDK范例大全上的一个例子,我这里是乱码,你也可以自己找找。最好先多看看资料吧。
package irdc.EX08_01;
/*ゲ惠まノapache.http闽摸ㄓミHTTP硈絬*/
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
/*ゲ惠まノjava.io 籔java.util闽摸ㄓ弄糶郎*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class EX08_01 extends Activity
{
/*ㄢButtonン,籔TextViewン*/
private Button mButton1,mButton2;
private TextView mTextView1;
/** Called when the activity is first created. */
@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://www.dubblogs.cc:8751/Android/Test/API/Post/index.php";
/*ミHTTP Post硈絬*/
HttpPost httpRequest = new HttpPost(uriAPI);
/*
* Post笲肚癳跑计ゲ斗ノNameValuePair[]皚纗
*/
List <NameValuePair> params = new ArrayList <NameValuePair>();
params.add(new BasicNameValuePair("str", "I am Post String"));
try
{
/*祇HTTP request*/
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);
}
else
{
mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString());
}
}
catch (ClientProtocolException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (IOException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (Exception e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
mButton2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
/*呼﹃*/
String uriAPI = "http://www.dubblogs.cc:8751/Android/Test/API/Get/index.php?str=I+am+Get+String";
/*ミHTTP Get硈絬*/
HttpGet httpRequest = new HttpGet(uriAPI);
try
{
/*祇HTTP request*/
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
/*璝篈絏200 ok*/
if(httpResponse.getStatusLine().getStatusCode() == 200)
{
/*莱﹃*/
String strResult = EntityUtils.toString(httpResponse.getEntity());
/*埃緇じ*/
strResult = eregi_replace("(\r\n|\r|\n|\n\r)","",strResult);
mTextView1.setText(strResult);
}
else
{
mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString());
}
}
catch (ClientProtocolException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (IOException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (Exception e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
}
/* 璹﹃ㄧ计 */
public String eregi_replace(String strFrom, String strTo, String strTarget)
{
String strPattern = "(?i)"+strFrom;
Pattern p = Pattern.compile(strPattern);
Matcher m = p.matcher(strTarget);
if(m.find())
{
return strTarget.replaceAll(strFrom, strTo);
}
else
{
return strTarget;
}
}
}
package irdc.EX08_01;
/*ゲ惠まノapache.http闽摸ㄓミHTTP硈絬*/
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
/*ゲ惠まノjava.io 籔java.util闽摸ㄓ弄糶郎*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class EX08_01 extends Activity
{
/*ㄢButtonン,籔TextViewン*/
private Button mButton1,mButton2;
private TextView mTextView1;
/** Called when the activity is first created. */
@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://www.dubblogs.cc:8751/Android/Test/API/Post/index.php";
/*ミHTTP Post硈絬*/
HttpPost httpRequest = new HttpPost(uriAPI);
/*
* Post笲肚癳跑计ゲ斗ノNameValuePair[]皚纗
*/
List <NameValuePair> params = new ArrayList <NameValuePair>();
params.add(new BasicNameValuePair("str", "I am Post String"));
try
{
/*祇HTTP request*/
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);
}
else
{
mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString());
}
}
catch (ClientProtocolException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (IOException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (Exception e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
mButton2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
/*呼﹃*/
String uriAPI = "http://www.dubblogs.cc:8751/Android/Test/API/Get/index.php?str=I+am+Get+String";
/*ミHTTP Get硈絬*/
HttpGet httpRequest = new HttpGet(uriAPI);
try
{
/*祇HTTP request*/
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
/*璝篈絏200 ok*/
if(httpResponse.getStatusLine().getStatusCode() == 200)
{
/*莱﹃*/
String strResult = EntityUtils.toString(httpResponse.getEntity());
/*埃緇じ*/
strResult = eregi_replace("(\r\n|\r|\n|\n\r)","",strResult);
mTextView1.setText(strResult);
}
else
{
mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString());
}
}
catch (ClientProtocolException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (IOException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (Exception e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
}
/* 璹﹃ㄧ计 */
public String eregi_replace(String strFrom, String strTo, String strTarget)
{
String strPattern = "(?i)"+strFrom;
Pattern p = Pattern.compile(strPattern);
Matcher m = p.matcher(strTarget);
if(m.find())
{
return strTarget.replaceAll(strFrom, strTo);
}
else
{
return strTarget;
}
}
}
解决方案 »
- 在别的线程调用主线程的handler
- 手机锁屏后cpu暂停了?对程序影响好严重..
- UI问题
- android 前辈们帮我看看这个异步加载图片哪里错了?
- DIY了一个ContentProvider,程序崩?
- 关于Android的一个程序问题,货币转换
- “writeFully: failed: Broken pipe”是哪里出错了?~
- AIDL进程间传递自定义类型参数
- 安卓原生跳转H5页面崩溃,请求大神指点
- android 实时播放网络摄像头的监控视频流程是什么,有没有demo参考啊
- Android源代码编译问题(添加自己定义的Java类)
- dialog的问题????????????????????????
public void downloadFile(String url, String filePath) { URL url2 = null; try { url2 = new URL(url); HttpURLConnection con = (HttpURLConnection) url2.openConnection(); InputStream in = con.getInputStream(); File fileOut = new File(filePath); FileOutputStream out = new FileOutputStream(fileOut); byte[] bytes = new byte[1024]; int c; while ((c = in.read(bytes)) != -1) { out.write(bytes, 0, c); } in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); }试过编码,但问题依旧,估计我写的编码有问题,谁帮我改改