gpt4 book ai didi

android - JSON 异常和权限

转载 作者:行者123 更新时间:2023-11-29 00:03:45 24 4
gpt4 key购买 nike

我已经使用开放天气 API 编写了天气应用程序....但是当我在我的手机中安装该应用程序并单击按钮以确定天气时它崩溃了...我在我的手机中使用 Android M

public class DownloadTask extends AsyncTask<String,Void,String>{

@Override
protected String doInBackground(String... urls) {

String result = "";
URL url;
HttpURLConnection httpURLConnection = null;

try {
url = new URL(urls[0]);
httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream in = httpURLConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while(data != -1){
char current = (char) data;
result += current;

data = reader.read();
}
return result;

}
//combined the exceptions MalformedURL and IOException to a common to display a toast msg
catch (Exception e) {
e.printStackTrace();
}

return null;

}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);

try {
String msg = "";
JSONObject jsonObject = new JSONObject(result);
String weatherInfo = jsonObject.getString("weather");

JSONArray arr = new JSONArray(weatherInfo);
for(int i = 0;i<arr.length();i++){
JSONObject jsonPart = arr.getJSONObject(i);
String main = "";
String desc = "";

main = jsonPart.getString("main");
desc = jsonPart.getString("description");
icon = jsonPart.getString("icon");

if (main != "" && desc != "") {
msg += main + "\r\n" + desc;
}

}

if(msg != ""){
weatherReport.setText(msg);
}
else{
Toast.makeText(MainActivity.this,
"Location not able to determine",Toast.LENGTH_LONG).show();
}

} catch (JSONException e) {
Toast.makeText(MainActivity.this,
"Location not able to determine",Toast.LENGTH_LONG).show();
e.printStackTrace();
}

}
}

显示的错误是 arr.length() 应用于空数组....我不明白错误是什么。是关于权限的吗...如果是的话我应该如何在 Marshmallow 中实现权限...这是 onCreate() 中的代码,如果它是关于权限的请告诉如何实现..

try {

//to hide the keyboard after pressing the button
InputMethodManager manager =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
manager.hideSoftInputFromInputMethod(weatherInput.getWindowToken(),0);

DownloadTask downloadTask = new DownloadTask();

//used to encode the entered input for url.. for example San Fransisco appears in url
//as San%20Fransisco ... and to enable that we use the encoder...
String encodedCity = URLEncoder.encode(city,"UTF-8");

downloadTask.execute("http://api.openweathermap.org/data/2.5/weather?q=" + encodedCity);

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

日志 ..

02-25 22:56:54.009 1413-1413/com.example.hemantj.weather E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hemantj.weather, PID: 1413
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
at org.json.JSONTokener.nextValue(JSONTokener.java:94)
at org.json.JSONObject.<init>(JSONObject.java:156)
at org.json.JSONObject.<init>(JSONObject.java:173)
at com.example.hemantj.weather.MainActivity$DownloadTask.onPostExecute(MainActivity.java:133)
at com.example.hemantj.weather.MainActivity$DownloadTask.onPostExecute(MainActivity.java:92)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.access$500(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

 try {
Log.d(TAG, "onPostExecute: this inner of post" + getcontent_for_validate);
jsonobj = new JSONObject(getcontent_for_validate);
System.out.println("this is get content" + jsonobj.toString());


JSONArray array = jsonobj.getJSONArray("Staff_Details");
for (int i = 0; i < array.length(); i++) {
Clint_id = editText_user_name.getText().toString();
Api_key = array.getJSONObject(i).getString("api_key");
COMPANY_LOGO = array.getJSONObject(i).getString("company_logo");
Password = editText_password.getText().toString();
}


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

仅限互联网权限

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

关于android - JSON 异常和权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42455140/

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