2015-05-09 148 views
0

我的HTTP客户端项目有一些问题,使用HTTP方法GET,问题是,当我在Android模拟器上运行我的应用程序时,它工作完美,当我在Java桌面应用程序中使用我的代码时,但是,当我导入APK文件并加载到我的Android设备上时,我根本不工作,我不知道为什么。Android上的HTTP客户端

类具有Get方法:

public class httpGetExClass { 
    public String getInternetData() throws Exception { 

     BufferedReader in = null; 
     String data = null; 
     try { 
      HttpClient client = new DefaultHttpClient(); 
      URI website = new URI("http://yumecms.com/absolvent/public/"); 
      HttpGet request = new HttpGet(); 
      request.setURI(website); 
      HttpResponse response = client.execute(request); in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
      StringBuffer sb = new StringBuffer(""); 
      String l = ""; 
      String nl = System.getProperty("line.separator"); 
      while ((l = in .readLine()) != null) { 
       sb.append(l + nl); 
      } in .close(); 
      data = sb.toString(); 
      return data; 
     } finally { 
      if (in != null) { 
       try { in .close(); 
        return data; 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 

    } 
} 

这是MainActivity

public class MainActivity extends Activity implements OnClickListener { 

    TextView tv1;@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     tv1 = (TextView) findViewById(R.id.textView1); 
    } 

    @Override 
    public void onClick(View arg0) { 
     httpGetExClass httpGet = new httpGetExClass(); 
     httpPostExClass httPost = new httpPostExClass(); 
     switch (arg0.getId()) { 
      case R.id.buttonEnter: 

       try { 
        Toast.makeText(this, "!!!!", Toast.LENGTH_SHORT).show(); 
        String returned = httpGet.getInternetData(); 
        Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show(); 
        tv1.setText(returned); 
       } catch (Exception e) { 
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); 
        e.printStackTrace(); 
       } 


       break; 
     } 

    } 
} 
+1

你可以发布日志吗? –

回答

1

你的代码被拦截,它不是在Android的允许。你必须为你的httpclient使用另一个线程或使用android专用的AsyncTask