繁体   English   中英

Android HTTP GET不起作用

[英]Android HTTP GET doesn't work

我知道Java,但很遗憾为Android开发选择了Basic4Android。 工作一年多后,我意识到我应该采用本机解决方案。 所以我的问题可能很愚蠢,但我需要您的建议来解决。

我的目标是从GET请求中检索数据。 我已经尝试了Internet上大量的android http客户端教程,但是每个教程都失败了。 我将在这里分享一个,以便您可以帮助我进行修复。 当我单击按钮时,在logcat窗口中没有大量错误消息就什么也没有发生。

Main Class在这里进行快速回顾:

public class MainActivity extends Activity implements OnClickListener {

    private Button Test;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Test = (Button) findViewById(R.id.Test);
        Test.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.Test){
            try {
                HttpClient client = new DefaultHttpClient();  
                String getURL = "http://www.google.com";
                HttpGet get = new HttpGet(getURL);
                HttpResponse responseGet = client.execute(get);  
                HttpEntity resEntityGet = responseGet.getEntity();  
                if (resEntityGet != null) {  
                    // do something with the response
                    String response = EntityUtils.toString(resEntityGet);
                    Log.i("GET RESPONSE", response);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            }
        }
    }

整个项目在这里: https : //dl.dropboxusercontent.com/u/15625012/TestHttp.zip

我将非常感谢任何形式的帮助/建议。

首先,始终在此处将错误发布在logcat中,我们几乎总是需要它们来解决问题。

但这很容易-您无法在主线程上进行网络IO。 您需要在AsyncTask或Thread上运行它。

    try {
        URL url = new URL(urlstr);
        HttpsURLConnection connection = 
                  (HttpsURLConnection)url.openConnection();
        connection.setReadTimeout(6000);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("User_agent", "android");
        OutputStream os = connection.getOutputStream();
        //os.write(buffer);

        InputStream is = connection.getInputStream();

    } catch (MalformedURLException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }catch (Exception e) {
        // TODO: handle exception
    }

您当前正在主UI线程中进行网络访问(按钮单击功能)。 Android不允许长时间运行操作,例如在应用程序的主线程UI线程中进行网络访问。 您需要在单独的线程中异步执行此操作。 为此,您可以使用内置的异步类。

这是我写的示例代码

public class Sample extends Activity 
{   
private ProgressDialog progress_dialog;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample);

    progress_dialog = new ProgressDialog(this);
}

public void MyButtonClick(View view)
{
        EditText usernameEditText = (EditText)findViewById(R.id.sample_username);
        String username = usernameEditText.getText();

        String URL = "http://SOME_WEBSITE?Username=" + username;

        progress_dialog.setMessage("Loading. Please wait...");
    progress_dialog.setCancelable(false);
    progress_dialog.show();

    new SampleAsynThread().execute(URL);
}

private class SampleAsynThreadextends AsyncTask <String, Void, String> 
{
        protected String doInBackground(String... urls)
        {
            // make your request here
            return "Response";
        }

        protected void onPostExecute(String result) 
        { 
            // show response on ui   
            progress_dialog.dismiss();
        }
    }

protected void onDestroy()
{
    progress_dialog.dismiss();
    super.onDestroy();
}

@Override
protected void onPause()
{
    progress_dialog.dismiss();
    super.onPause();
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM