Http获取使用Android HttpURLConnection

我不熟悉Java和Android开发,并尝试创build一个简单的应用程序,它应该联系一个Web服务器,并使用http get将一些数据添加到数据库。

当我在电脑上使用networking浏览器进行通话时,它工作得很好。 但是,当我在Android模拟器上执行应用程序的调用时,不会添加任何数据。

我已经将Internet权限添加到应用的清单中。 Logcat不报告任何问题。

任何人都可以帮我弄清楚什么是错的?

这里是源代码:

package com.example.httptest; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class HttpTestActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); setContentView(tv); try { URL url = new URL("http://www.mysite.se/index.asp?data=99"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.disconnect(); tv.setText("Hello!"); } catch (MalformedURLException ex) { Log.e("httptest",Log.getStackTraceString(ex)); } catch (IOException ex) { Log.e("httptest",Log.getStackTraceString(ex)); } } } 

尝试从中获取inputstream,然后获取文本数据。

  URL url; HttpURLConnection urlConnection = null; try { url = new URL("http://www.mysite.se/index.asp?data=99"); urlConnection = (HttpURLConnection) url .openConnection(); InputStream in = urlConnection.getInputStream(); InputStreamReader isw = new InputStreamReader(in); int data = isw.read(); while (data != -1) { char current = (char) data; data = isw.read(); System.out.print(current); } } catch (Exception e) { e.printStackTrace(); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } 

您也可以使用其他inputstream读取器,例如缓冲读取器。

问题是,当你打开连接 – 它不会“拉”任何数据。

这是一个完整的AsyncTask

 public class GetMethodDemo extends AsyncTask<String , Void ,String> { String server_response; @Override protected String doInBackground(String... strings) { URL url; HttpURLConnection urlConnection = null; try { url = new URL(strings[0]); urlConnection = (HttpURLConnection) url.openConnection(); int responseCode = urlConnection.getResponseCode(); if(responseCode == HttpURLConnection.HTTP_OK){ server_response = readStream(urlConnection.getInputStream()); Log.v("CatalogClient", server_response); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Log.e("Response", "" + server_response); } } // Converting InputStream to String private String readStream(InputStream in) { BufferedReader reader = null; StringBuffer response = new StringBuffer(); try { reader = new BufferedReader(new InputStreamReader(in)); String line = ""; while ((line = reader.readLine()) != null) { response.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return response.toString(); } 

调用这个AsyncTask

 new GetMethodDemo().execute("your web-service url"); 

我已经创build了对Activity类的callBack(委托)响应。

 public class WebService extends AsyncTask<String, Void, String> { private Context mContext; private OnTaskDoneListener onTaskDoneListener; private String urlStr = ""; public WebService(Context context, String url, OnTaskDoneListener onTaskDoneListener) { this.mContext = context; this.urlStr = url; this.onTaskDoneListener = onTaskDoneListener; } @Override protected String doInBackground(String... params) { try { URL mUrl = new URL(urlStr); HttpURLConnection httpConnection = (HttpURLConnection) mUrl.openConnection(); httpConnection.setRequestMethod("GET"); httpConnection.setRequestProperty("Content-length", "0"); httpConnection.setUseCaches(false); httpConnection.setAllowUserInteraction(false); httpConnection.setConnectTimeout(100000); httpConnection.setReadTimeout(100000); httpConnection.connect(); int responseCode = httpConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader br = new BufferedReader(new InputStreamReader(httpConnection.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); return sb.toString(); } } catch (IOException e) { e.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if (onTaskDoneListener != null && s != null) { onTaskDoneListener.onTaskDone(s); } else onTaskDoneListener.onError(); } } 

哪里

 public interface OnTaskDoneListener { void onTaskDone(String responseData); void onError(); } 

你可以根据你的需要修改。 这是为了得到

如果您只需要一个非常简单的调用,则可以直接使用URL:

 import java.net.URL; new URL("http://wheredatapp.com").openStream(); 

url=新url(“ https://www.google.com ”);

//如果你正在使用

URLConnection conn = url.openConnection();

//将其更改为

HttpURLConnection conn =(HttpURLConnection)url.openConnection();