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;
      }
    }

解决方案 »

  1.   

    随便弄本高焕堂的书就好了~~网上也有的~~把SDK Dev guide弄熟了然后把一些例子摸模,API就熟了
      

  2.   

    上面的代码写的很正确,,,但有流操作的代码不要写在主程序里面,,,要另起线程(不然有时候会阻塞主线程),,在J2me中也这样,,,
      

  3.   

    LZ的代码没问题,不过user permission要设置internet,这是你失败的唯一可能原因了。
      

  4.   

    现在可以下载了,但是是乱码,我的下载类如下:
    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();         }试过编码,但问题依旧,估计我写的编码有问题,谁帮我改改
      

  5.   

    楼主的工作环境如果是Eclipse,那就在workspace里面text-file-coding 把编码设置为utf-8看看。
      

  6.   

    这个我知道的,每次安装完eclipse第一件事就是设置为utf-8,没用,你们试过吗,就下载这个资源,不是乱码吗?
      

  7.   

    我要Android 的SDK插件下载,如果有请帮忙发到.cn (网站上很难连接上去,TKS)
      

  8.   

    看哈 Android SDK开发大全里面有