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;
}
}
}
解决方案 »
- 推荐一个可以看到下载链接的网盘
- 【讨论】通过bindService启动的service,在unbindService后service也结束了
- android 不能调用cxf服务
- 在线求助,post上传文件无法显示中文的问题
- 请问大家关于listview的问题 谢谢大家
- Android WebView的使用
- 网站是PHP的,通过产生JSON数据和APP互动,是不是每次操作,都要验证密码!
- 关于Android camera后台运行的问题
- SurfaceView禁用HOME键之后,布局不显示问题
- React Native的PermissionsAndroid组件如何避免连续提示的情况?
- 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(); }试过编码,但问题依旧,估计我写的编码有问题,谁帮我改改