[英]Creating a JSON file from a url
Hi guys I have a problem creating a JSON file from a google url that i have. 嗨,大家好,我在从Google网址创建JSON文件时遇到问题。 This is my code that im using.
这是我正在使用的代码。
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadUrl {
public String readUrl(String strUrl) throws IOException, InterruptedException {
Log.d("URLS = ",strUrl);
Thread.sleep(2000);
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
data = sb.toString();
Log.d("downloadUrl", data.toString());
br.close();
} catch (Exception e) {
Log.d("Exception", e.toString());
} finally {
iStream.close();
urlConnection.disconnect();
}
return data;
}
} }
It works fine when i throw a url that looks like this into it. 当我将看起来像这样的网址扔进去时,它工作正常。 https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.7207523,-73.383851&radius=4828&type=bar&key=MYKEY
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.7207523,-73.383851&radius=4828&type=bar&key=MYKEY
But when i try and throw a url that looks like this into it. 但是,当我尝试将看起来像这样的网址扔进去时。 https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJe3AmoGsr6IkRuWcK1LAh-DE&key=MYKEY
https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJe3AmoGsr6IkRuWcK1LAh-DE&key=MYKEY
I get an error: D/GooglePlacesReadTask: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.InputStream.close()' on a null object reference 我收到错误消息:D / GooglePlacesReadTask:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void java.io.InputStream.close()'
I dont know how i fix this. 我不知道我该如何解决。 Any help?
有什么帮助吗?
Aah you did not mention this is in android, I presume this because you said , android.os.NetworkOnMainThreadException in your comment 啊,您没有提到这是在android中,我认为这是因为您在评论中说了android.os.NetworkOnMainThreadException
Android does not allow time consuming tasks on main thread, use AsyncTask to call your function or use plain old java thread Android不允许在主线程上执行耗时的任务,请使用AsyncTask调用您的函数或使用普通的旧Java线程
Network on main thread exception comes when you run a networking operation on main thread . 当您在主线程上运行网络操作时,主线程上的网络异常会出现。 Generally AsyncTask is used for these works but if you want to use the same code you written Just add..
通常,AsyncTask用于这些工作,但如果要使用您编写的相同代码,只需添加即可。
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.