Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.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
Java 从Json解析数据时出现问题Google将API放置到recyclerView_Java_Android_Json_Parsing_Google Places Api - Fatal编程技术网

Java 从Json解析数据时出现问题Google将API放置到recyclerView

Java 从Json解析数据时出现问题Google将API放置到recyclerView,java,android,json,parsing,google-places-api,Java,Android,Json,Parsing,Google Places Api,我想向用户显示他们附近的酒吧列表,没有地图。我尝试过PlaceLikelihoods,它很好用,但我不能对找到的地点类型进行任何限制,它显示了一张地图 所以我按URL搜索,但是Try/Catch总是导致异常,在那里找到的toast显示了良好的结果,但是recyclerview列表没有填充。 我发现了一个类似的问题,即要移动notifyDataSetChanged(),我已经完成了 我发现掌握API和解析非常复杂,所以这可能是一个愚蠢的错误,但已经2周了,我还没有成功。 谢谢你的阅读 public

我想向用户显示他们附近的酒吧列表,没有地图。我尝试过PlaceLikelihoods,它很好用,但我不能对找到的地点类型进行任何限制,它显示了一张地图

所以我按URL搜索,但是Try/Catch总是导致异常,在那里找到的toast显示了良好的结果,但是recyclerview列表没有填充。 我发现了一个类似的问题,即要移动notifyDataSetChanged(),我已经完成了

我发现掌握API和解析非常复杂,所以这可能是一个愚蠢的错误,但已经2周了,我还没有成功。 谢谢你的阅读

public class TestMap extends AppCompatActivity {

// CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
public static final int CONNECTION_TIMEOUT = 10000;
public static final int READ_TIMEOUT = 15000;
private LocListAdapter mAdapter;
private EmptyStateRecyclerView mLocRecView;
private ArrayList<LocListUser> data;

private double longitudeUser;
private double latitudeUser;

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

    data = new ArrayList<>();

    mAdapter = new LocListAdapter(data, TestMap.this);
    mLocRecView = findViewById(R.id.locRecView);
    mLocRecView.setItemAnimator(new DefaultItemAnimator());
    mLocRecView.setLayoutManager(new LinearLayoutManager(TestMap.this));
    mLocRecView.setHasFixedSize(true);
    //data.clear();
    mLocRecView.setAdapter(mAdapter);
    mLocRecView.clearStateDisplays();

    longitudeUser = getIntent().getDoubleExtra("long", 151.2106085);
    latitudeUser = getIntent().getDoubleExtra("lat", -33.8523341);

    new AsyncFetch().execute();
}

private class AsyncFetch extends AsyncTask<String, String, String> {
    ProgressDialog pdLoading = new ProgressDialog(TestMap.this);
    HttpURLConnection conn;
    URL url = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pdLoading.setMessage("\tLoading...");
        pdLoading.setCancelable(false);
        pdLoading.show();
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            StringBuilder sb;
            sb = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
            sb.append("location=").append(latitudeUser).append(",").append(longitudeUser);
            sb.append("&radius=5000");
            sb.append("&types=" + "bar");
            sb.append("&key=API_KEY");
            url = new URL(String.valueOf(sb));

        } catch (MalformedURLException e) {
            e.printStackTrace();
            return e.toString();
        }
        try {
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("GET");
            conn.setDoOutput(true);

        } catch (IOException e1) {
            e1.printStackTrace();
            return e1.toString();
        }
        try {
            int response_code = conn.getResponseCode();
            if (response_code == HttpURLConnection.HTTP_OK) {
                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
                StringBuilder result = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }
                // Pass data to onPostExecute method
                return (result.toString());

            } else {
                return ("unsuccessful");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return e.toString();
        } finally {
            conn.disconnect();
        }
    }

    @Override
    protected void onPostExecute(String result) {
        pdLoading.dismiss();
        try {
            JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                LocListUser locList = new LocListUser();
                locList.setAddress(json_data.getString("vicinity"));
                locList.setId(json_data.getString("reference"));
                locList.setName(json_data.getString("place_name"));
                locList.setLat(json_data.getJSONObject("geometry").getJSONObject("location").getString("lat"));
                locList.setLon(json_data.getJSONObject("geometry").getJSONObject("location").getString("lng"));
                locList.setType(json_data.getString("types"));
                locList.setLatUser(latitudeUser);
                locList.setLonUser(longitudeUser);
                data.add(locList);
                mAdapter.notifyDataSetChanged();
            }
            //mAdapter.notifyDataSetChanged();
            mLocRecView.invokeState(EmptyStateRecyclerView.STATE_OK);
        } catch (JSONException e) { //e.toString()
            e.printStackTrace();
        }
    }
}
公共类TestMap扩展了AppCompative活动{
//连接超时和读取超时以毫秒为单位
公共静态最终int连接\u超时=10000;
公共静态最终整型读取超时=15000;
私人住宅;
私有EmptySateRecyclView mLocRecView;
私有数组列表数据;
私人双纵机;
私人双纬度;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动位置);
数据=新的ArrayList();
mAdapter=newloclistadapter(数据,TestMap.this);
mLocRecView=findviewbyd(R.id.locRecView);
setItemAnimator(新的DefaultItemAnimator());
setLayoutManager(新的LinearLayoutManager(TestMap.this));
mLocRecView.setHasFixedSize(true);
//data.clear();
mLocRecView.setAdapter(mAdapter);
mLocRecView.clearStateDisplays();
longitudeUser=getIntent().getDoubleExtra(“long”,151.2106085);
latitudeUser=getIntent().getDoubleExtra(“lat”,-33.8523341);
新建AsyncFetch().execute();
}
私有类AsyncFetch扩展了AsyncTask{
ProgressDialog pdLoading=新建ProgressDialog(TestMap.this);
httpurl连接连接;
URL=null;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pdLoading.setMessage(“\t加载…”);
pdLoading.setCancelable(假);
pdLoading.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
为某人做准备;
sb=新的StringBuilder(“https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
sb.append(“location=”).append(latitudeUser).append(“,”).append(longitudeUser);
sb.追加(“&radius=5000”);
sb.追加(“&types=“+”条”);
sb.追加(“&key=API_key”);
url=新url(String.valueOf(sb));
}捕获(格式错误){
e、 printStackTrace();
返回e.toString();
}
试一试{
conn=(HttpURLConnection)url.openConnection();
conn.setReadTimeout(读取超时);
连接设置连接超时(连接超时);
conn.setRequestMethod(“GET”);
连接设置输出(真);
}捕获(IOE1异常){
e1.printStackTrace();
返回e1.toString();
}
试一试{
int response_code=conn.getResponseCode();
if(response\u code==HttpURLConnection.HTTP\u OK){
InputStream input=conn.getInputStream();
BufferedReader=new BufferedReader(new InputStreamReader(input,StandardCharsets.UTF_8));
StringBuilder结果=新建StringBuilder();
弦线;
而((line=reader.readLine())!=null){
结果。追加(行);
}
//将数据传递给onPostExecute方法
返回(result.toString());
}否则{
返回(“未成功”);
}
}捕获(IOE异常){
e、 printStackTrace();
返回e.toString();
}最后{
连接断开();
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
pdLoading.disclose();
试一试{
JSONArray jArray=新JSONArray(结果);
for(int i=0;i

}您正在使用硬编码字符串

 JSONArray jArray = new JSONArray("results");
你必须使用

JSONArray jArray = new JSONArray(result);

问题还是错误?很抱歉,我不明白其中的区别,我不知道。您遇到了什么异常?#noob,error,typemissmatch与Jsonarray。我比较了几篇教程,似乎是正确的,我不明白问题来自何处。我再次输入了结果(我在测试期间更改了结果),我有相同的错配异常,并且没有可见数据。所以我只是在我的bug上添加了一个bug——”,谢谢,对另一个错误有什么想法吗?