- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我发送下载请求时,我遇到了 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/
我的应用程序从网络服务加载大量数据,我正在使用 volley 库处理网络请求。 Volley 正在堆叠 NetworkDispatcher 对象并最终导致应用程序崩溃。 我用 DDMS 堆转储 (ht
在我的程序中有 NetworkDispatcher.run 错误我正在使用 volley 库。我的程序部分是。我一直在做,但没有任何效果请帮我做我的程序。我搜索了很多网络内容,但没有任何帮助。我正在做
我正在尝试运行以下代码: Map requestMap = new HashMap<>(); Long unixTime = System.currentTimeMillis() / LONG_100
您好,我正在尝试运行以下代码将数据发布到在线 MySQL 数据库 if (data.getCount() > 0) { data.moveToFirst
我收到以下错误: NetworkDispatcher.run: Unhandled exception java.lang.SecurityException: Permission denied (
当我发送下载请求时,我遇到了 java volley 的问题,它给出了一个错误 Bad URL null,我不明白它为什么这样做。我正在使用开放天气图来下载 json。该代码在点击方法中设置 url,
这个问题在这里已经有了答案: What is a NullPointerException, and how do I fix it? (12 个答案) 关闭 6 年前。 错误“”NetworkDi
我是一名优秀的程序员,十分优秀!