【问题标题】:json data isn't displayed in my app. i did everything as per instructions but still couldn't get the json data displayedjson 数据未显示在我的应用程序中。我按照说明做了所有事情,但仍然无法显示 json 数据
【发布时间】:2020-12-11 18:53:24
【问题描述】:

它基本上是一个初学者级别的天气应用程序。这是我在应用程序主要活动中的代码。
每个库都是导入的。我在各个地方进行了检查。我已经完全按照他们所说的那样实现了,但是应用程序中似乎没有显示任何内容此外,还实现了依赖项,即 ---> implementation 'com.android.volley:volley: 1.1.1'。

package susinth.josh.weatherapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
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.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    private TextView text_temp;
    private TextView text_city;
    private TextView text_description;
    private TextView text_date;


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

        text_temp=findViewById(R.id.txtTemp);
        text_city=findViewById(R.id.txtCity);
        text_description=findViewById(R.id.txtDesc);
        text_date=findViewById(R.id.txtDate);

        find_weather();
    }



    public void find_weather() {
        String url="http://api.openweathermap.org/data/2.5/weather?q=London&appid=01138a7db759c13b7b0a1390ed862fff&units=metric";
        JsonObjectRequest jor= new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                try {
                    JSONObject main_object=response.getJSONObject("main");
                    JSONArray weatherArray=response.getJSONArray("weather");
                    JSONObject object=weatherArray.getJSONObject(0);

                    String temp= String.valueOf(main_object.getDouble("temp"));
                    String description=object.getString("description");
                    String city=response.getString("name");
                    System.out.println(temp);
                    Calendar calendar=Calendar.getInstance();
                    SimpleDateFormat sdf=new SimpleDateFormat("EEEE-MM-dd");
                    String formattedDate=sdf.format(calendar.getTime());

                    text_date.append(formattedDate);
                    text_city.setText(city);
                    text_description.setText(description);
                    text_temp.setText(temp);


                } catch (JSONException e) {
                    e.printStackTrace();

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        RequestQueue queue= Volley.newRequestQueue(this);
        queue.add(jor);
    }
}

我的 json 数据

{
     "coord": {
       "lon": -0.13,
       "lat": 51.51
     },
     "weather": [
       {
         "id": 300,
         "main": "Drizzle",
         "description": "light intensity drizzle",
         "icon": "09d"
       }
     ],
     "base": "stations",
     "main": {
       "temp": 280.32,
       "pressure": 1012,
       "humidity": 81,
       "temp_min": 279.15,
       "temp_max": 281.15
     },
     "visibility": 10000,
     "wind": {
       "speed": 4.1,
       "deg": 80
     },
     "clouds": {
       "all": 90
     },
     "dt": 1485789600,
     "sys": {
       "type": 1,
       "id": 5091,
       "message": 0.0103,
       "country": "GB",
       "sunrise": 1485762037,
       "sunset": 1485794875
     },
     "id": 2643743,
     "name": "London",
     "cod": 200
     }

【问题讨论】:

    标签: java json android-studio android-volley


    【解决方案1】:

    您的代码设置看起来不错,并且使用干净的 Android/Java 项目重现此代码,您的项目可能会遇到 2 个问题。所有信息都通过此代码打印在Logcat 控制台中:

    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
    }
    

    第一个可能的问题

    最初返回此Errorcom.android.volley.NoConnectionError: java.io.IOException: Cleartext HTTP traffic to api.openweathermap.org not permitted;

    这个Error 可以通过更改API url 以使用HTTPS 而不是HTTP 协议来解决。所以你最终得到:

    String url="http://api.openweathermap.org/data/2.5/weather?q=London&amp;appid=01138a7db759c13b7b0a1390ed862fff&amp;units=metric";

    在此处阅读有关 Cleartext 错误的更多信息:Android 8: Cleartext HTTP traffic not permitted

    第二个可能的问题

    确保您已将 Internet 权限添加到您的 AndroidManifest.xml:

    <uses-permission android:name="android.permission.INTERNET" />
    

    假设您的布局工作正常,这应该可以解决在屏幕上显示数据的问题。

    【讨论】:

    • 非常感谢。我将http更改为https。它奏效了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2022-01-18
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    相关资源
    最近更新 更多