Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android无法使用HttpPost-java.net.UnknownHostException解析主机_Android - Fatal编程技术网

Android无法使用HttpPost-java.net.UnknownHostException解析主机

Android无法使用HttpPost-java.net.UnknownHostException解析主机,android,Android,在我的应用程序中,我用HttpPost解析json,如下所示 ` 我的清单文件已正确设置为internet访问 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> 事实上我找到了解决办法。这条线 HttpPost httpGet = new

在我的应用程序中,我用HttpPost解析json,如下所示

`

我的清单文件已正确设置为internet访问

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.INTERNET" />

事实上我找到了解决办法。这条线

HttpPost httpGet = new HttpPost(url);
必须用HttpGet as替换

 HttpGet httpGet = new HttpGet(url);

我发现最好的一个是android开发者培训,下面是链接

连接并下载数据

// Given a URL, establishes an HttpUrlConnection and retrieves // the web page content as a InputStream, which it returns as a string. private String downloadUrl(String myurl) throws IOException { InputStream is = null; // Only display the first 500 characters of the retrieved // web page content. int len = 500; try { URL url = new URL(myurl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000 /* milliseconds */); conn.setConnectTimeout(15000 /* milliseconds */); conn.setRequestMethod("GET"); conn.setDoInput(true); // Starts the query conn.connect(); int response = conn.getResponseCode(); Log.d(DEBUG_TAG, "The response is: " + response); is = conn.getInputStream(); // Convert the InputStream into a string String contentAsString = readIt(is, len); return contentAsString; // Makes sure that the InputStream is closed after the app is // finished using it. } finally { if (is != null) { is.close(); } } } //给定URL,建立HttpUrlConnection并检索 //网页内容以InputStream的形式显示,并以字符串的形式返回。 私有字符串下载URL(字符串myurl)引发IOException{ InputStream=null; //仅显示检索到的文件的前500个字符 //网页内容。 int len=500; 试一试{ URL=新URL(myurl); HttpURLConnection conn=(HttpURLConnection)url.openConnection(); conn.setReadTimeout(10000/*毫秒*/); conn.setConnectTimeout(15000/*毫秒*/); conn.setRequestMethod(“GET”); conn.setDoInput(真); //启动查询 连接(); int response=conn.getResponseCode(); Log.d(DEBUG_标记,“响应为:”+response); is=conn.getInputStream(); //将InputStream转换为字符串 字符串contentAsString=readIt(is,len); 返回contentAsString; //确保在应用程序启动后关闭InputStream //用完了。 }最后{ 如果(is!=null){ is.close(); } } } 将InputStream转换为字符串

// Reads an InputStream and converts it to a String. public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException { Reader reader = null; reader = new InputStreamReader(stream, "UTF-8"); char[] buffer = new char[len]; reader.read(buffer); return new String(buffer); } //读取InputStream并将其转换为字符串。 公共字符串readIt(InputStream stream,int len)引发IOException、UnsupportedEncodingException{ Reader=null; 读卡器=新的InputStreamReader(流,“UTF-8”); char[]buffer=新字符[len]; 读(缓冲区); 返回新字符串(缓冲区); }
从浏览器中启动查询。-android浏览器在android emulator浏览器中键入URL,并查看结果是否出现。。。有时仿真器无法连接到Internet当您说“有时仅”时,它是否可能在您的手机上工作,但在仿真器中不工作?或者它“有时在手机中工作”和“有时在模拟器中工作”?您在哪里运行上述代码?在活动或服务中? // Given a URL, establishes an HttpUrlConnection and retrieves // the web page content as a InputStream, which it returns as a string. private String downloadUrl(String myurl) throws IOException { InputStream is = null; // Only display the first 500 characters of the retrieved // web page content. int len = 500; try { URL url = new URL(myurl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000 /* milliseconds */); conn.setConnectTimeout(15000 /* milliseconds */); conn.setRequestMethod("GET"); conn.setDoInput(true); // Starts the query conn.connect(); int response = conn.getResponseCode(); Log.d(DEBUG_TAG, "The response is: " + response); is = conn.getInputStream(); // Convert the InputStream into a string String contentAsString = readIt(is, len); return contentAsString; // Makes sure that the InputStream is closed after the app is // finished using it. } finally { if (is != null) { is.close(); } } } // Reads an InputStream and converts it to a String. public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException { Reader reader = null; reader = new InputStreamReader(stream, "UTF-8"); char[] buffer = new char[len]; reader.read(buffer); return new String(buffer); }