E / Volley:[6681] NetworkDispatcher.processRequest:未处理的异常java.lang.RuntimeException:错误的URL null

时间:2018-11-26 13:00:01

标签: java android

当我发送请求下载它时,我遇到了Java Volley的问题,该错误给出错误的URL null null,我不明白为什么这样做。 我正在使用开放天气地图供json下载。 该代码会在on click方法中设置网址,然后调用下面的JSON下载器代码。

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

上述LocationID的示例为2657832,以生成网址: 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,这是一个技巧,这是我们从课程工作中获得的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);
}
}

谢谢您的任何建议。依云

1 个答案:

答案 0 :(得分:0)

 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);
            }
        });
    }
};

已修复问题,现在可以从办公室免费下载数据。