gpt4 book ai didi

java - E/Volley : [6681] NetworkDispatcher. processRequest : Unhandled exception java. lang.RuntimeException: Bad URL null

转载 作者:行者123 更新时间:2023-11-30 05:12:33 24 4
gpt4 key购买 nike

当我发送下载请求时,我遇到了 java volley 的问题,它给出了一个错误 Bad URL null,我不明白它为什么这样做。我正在使用开放天气图来下载 json。该代码在点击方法中设置 url,然后调用下面的 JSON 下载程序代码。

public String Weather = "api.openweathermap.org/data/2.5/forecast?id=" + LocationID + "&APPID=99b1346cd10b7389a4592e7f3123a412&units=imperial";

上述 LocationID 的一个例子是 2657832 制作 url:api.openweathermap.org/data/2.5/forecast?id=2657832&APPID=99b1346cd10b7389a4592e7f3123a412&units=imperial如果你在网络浏览器中输入它,它会起作用

 JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, webAdress,
null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
String RESP = response.toString();
TEXT.setText("Response: " + RESP);
Log.d("TAG", "onResponse: " + webAdress);
Log.d("RESP", "onResponse: " + RESP);
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
TEXT.setText("That didn't work!");
Log.d("TAG", "onErrorResponse: " + webAdress);
}
});

设置URL的点击方法

@Override
public void onClick(View v){

switch (v.getId()) {

case R.id.BTN_fav1:
LocationID = fave1S;
Weather = "api.openweathermap.org/data/2.5/forecast?id=" + LocationID + "&APPID=99b1346cd10b7389a4592e7f3123a412";
webAdress = Weather;

queue.add(jsonObjectRequest);
Log.d("TAG", "onClick: " + LocationID + " " + webAdress);

break;

但如果我将 json 请求中的 URL 设置为 JOSN_WEB 而不是 webAdress,这是一个更重要的问题,这是一个我们从类(class)作业中获得的 url,它可以工作

public String JSON_WEB = "http://datapoint.metoffice.gov.uk/public/data/val/wxfcs/all/json/sitelist?key=0732c29d-95d4-41e3-9903-5a9244b3cd5b";

完整代码:

package com.example.ewan.coursework;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Spinner;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

enter code herepublic class MainActivity extends AppCompatActivity implements View.OnClickListener {

public static final String SHARED_PREFS = "SHARED_PREFS";

public static final String fave1 = "Fave1";
public static final String fave2 = "Fave2";
public static final String fave3 = "Fave3";
public static final String fave4 = "Fave4";
public static final String fave5 = "Fave5";

private String fave1S;
private String fave2S;
private String fave3S;
private String fave4S;
private String fave5S;

Button BTN_fav1;
Button BTN_fav2;
Button BTN_fav3;
Button BTN_fav4;
Button BTN_fav5;

public String LocationName;
public String LocationID;
public String JSON_WEB = "http://datapoint.metoffice.gov.uk/public/data/val/wxfcs/all/json/sitelist?key=0732c29d-95d4-41e3-9903-5a9244b3cd5b";
public String Weather = "api.openweathermap.org/data/2.5/forecast?id=" + LocationID + "&APPID=99b1346cd10b7389a4592e7f3123a412&units=imperial";
public String webAdress;
public String[] location;
public String[] favourites;
public Spinner SP_location;
RequestQueue queue;
TextView TEXT;
AutoCompleteTextView ACTV;
Button BTN_search;
Button BTN_settings;

private static final String Key_Value = "value";


@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(Key_Value,String.valueOf(TEXT.getText()));
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

BTN_fav1 = findViewById(R.id.BTN_fav1);
BTN_fav2 = findViewById(R.id.BTN_fav2);
BTN_fav3 = findViewById(R.id.BTN_fav3);
BTN_fav4 = findViewById(R.id.BTN_fav4);
BTN_fav5 = findViewById(R.id.BTN_fav5);

BTN_fav1.setOnClickListener(this);
BTN_fav2.setOnClickListener(this);
BTN_fav3.setOnClickListener(this);
BTN_fav4.setOnClickListener(this);
BTN_fav5.setOnClickListener(this);

BTN_search = findViewById(R.id.BTN_search);
BTN_search.setOnClickListener(this);

BTN_settings = findViewById(R.id.BTN_settings);
BTN_settings.setOnClickListener(this);

TEXT = findViewById(R.id.TV_result_test);

Resources res = getResources();
location = res.getStringArray(R.array.locations);

ACTV = findViewById(R.id.ACTV_search);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1,location);
ACTV.setAdapter(adapter);

queue = Volley.newRequestQueue(this);

if (savedInstanceState != null) {
TEXT.setText(savedInstanceState.getString(Key_Value));
}

loadData();
updateViews();

try{URL url = new URL(webAdress);} catch (MalformedURLException e) {
e.printStackTrace();
}
}



@Override
public void onClick(View v){

switch (v.getId()) {

case R.id.BTN_fav1:
LocationID = fave1S;
Weather = "api.openweathermap.org/data/2.5/forecast?id=" + LocationID + "&APPID=99b1346cd10b7389a4592e7f3123a412";
webAdress = Weather;

queue.add(jsonObjectRequest);
Log.d("TAG", "onClick: " + LocationID + " " + webAdress);

break;

case R.id.BTN_fav2:
LocationID = fave2S;
Weather = "api.openweathermap.org/data/2.5/forecast?id=\" + LocationID + \"&APPID=99b1346cd10b7389a4592e7f3123a412&units=imperial";
webAdress = Weather;

queue.add(jsonObjectRequest);
Log.d("TAG", "onClick: " + webAdress);
break;

case R.id.BTN_fav3:
LocationID = fave3S;
Weather = "api.openweathermap.org/data/2.5/forecast?id=" + LocationID + "&APPID=99b1346cd10b7389a4592e7f3123a412&units=imperial";
webAdress = Weather;

queue.add(jsonObjectRequest);
Log.d("TAG", "onClick: " + webAdress);
break;

case R.id.BTN_fav4:
LocationID = fave4S;
Weather = "api.openweathermap.org/data/2.5/forecast?id=" + LocationID + "&APPID=99b1346cd10b7389a4592e7f3123a412&units=imperial";
webAdress = Weather;

queue.add(jsonObjectRequest);
Log.d("TAG", "onClick: " + webAdress);
break;

case R.id.BTN_fav5:
LocationID = fave5S;
Weather = "api.openweathermap.org/data/2.5/forecast?id=" + LocationID + "&APPID=99b1346cd10b7389a4592e7f3123a412&units=imperial";
webAdress = Weather;

queue.add(jsonObjectRequest);
Log.d("TAG", "onClick: " + webAdress);
break;

case R.id.BTN_search:
LocationID = ACTV.getText().toString();
Weather = "api.openweathermap.org/data/2.5/forecast?id=" + LocationID + "&APPID=99b1346cd10b7389a4592e7f3123a412&units=imperial";
webAdress = Weather;

queue.add(jsonObjectRequest);
Log.d("TAG", "onClick: " + webAdress);
break;

case R.id.BTN_settings:
startActivity(new Intent(MainActivity.this,settings.class));
break;
}
}

StringRequest stringRequest = new StringRequest(Request.Method.GET, JSON_WEB,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
TEXT.setText("Response is: " + response.substring(0, 500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
TEXT.setText("That didn't work!");
}
});


JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, webAdress,
null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
String RESP = response.toString();
TEXT.setText("Response: " + RESP);
Log.d("TAG", "onResponse: " + webAdress);
Log.d("RESP", "onResponse: " + RESP);
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
TEXT.setText("That didn't work!");
Log.d("TAG", "onErrorResponse: " + webAdress);
}
});


public void loadData()
{
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
fave1S = sharedPreferences.getString(fave1, "Favorite not set");
fave2S = sharedPreferences.getString(fave2, "Favorite not set");
fave3S = sharedPreferences.getString(fave3, "Favorite not set");
fave4S = sharedPreferences.getString(fave4, "Favorite not set");
fave5S = sharedPreferences.getString(fave5, "Favorite not set");
Log.d("tag", "loadData: " + fave1 + " saved as " + fave1S);
}
public void updateViews()
{
BTN_fav1.setText(fave1S);
BTN_fav2.setText(fave2S);
BTN_fav3.setText(fave3S);
BTN_fav4.setText(fave4S);
BTN_fav5.setText(fave5S);
}
}

感谢您的任何建议。伊万

最佳答案

 final ThreadLocal<JsonObjectRequest> jsonObjectRequest = new ThreadLocal<JsonObjectRequest>() {
@Override
protected JsonObjectRequest initialValue() {
return new JsonObjectRequest(Request.Method.GET, webAdress,
null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
RESP = response.toString();
TEXT.setText("Response: " + RESP);
Log.d("TAG", "onResponse: " + webAdress);
Log.d("RESP", "onResponse: " + RESP);
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
TEXT.setText("That didn't work!");
Log.d("TAG", "onErrorResponse: " + webAdress);
}
});
}
};

修复了问题,现在可以从 met office 下载数据了。

关于java - E/Volley : [6681] NetworkDispatcher. processRequest : Unhandled exception java. lang.RuntimeException: Bad URL null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53481679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com