0
我已經使用開放API的天氣和崩潰當我按一下按鈕,以確定天氣創造了一個天氣應用程序..下面天氣API是logcat的JSON錯誤
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)
下面是代碼...我已經使用ANDROID TARGET SDK 25和MIN SDK爲23 ..所以棉花糖參與..所以如果涉及到權限什麼是錯的也點出來...
weather.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(checkSelfPermission(Manifest.permission.INTERNET) == PackageManager.PERMISSION_GRANTED){
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 + "&APPID=0a7808bb8061814df9b6d5fc88d58b8f");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
else{
if(shouldShowRequestPermissionRationale(Manifest.permission.INTERNET)){
Toast.makeText(MainActivity.this,
"Internet permissions are necessary",Toast.LENGTH_LONG).show();
}
requestPermissions(new String[]{Manifest.permission.INTERNET},INTERNET_CODE);
}
}
});
}
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 message = "";
JSONObject jsonObject = new JSONObject(result);
String weatherInfo = jsonObject.getString("weather");
Log.i("Weather content", weatherInfo);
JSONArray arr = new JSONArray(weatherInfo);
for (int i = 0; i < arr.length(); i++) {
JSONObject jsonPart = arr.getJSONObject(i);
String main = "";
String description = "";
main = jsonPart.getString("main");
description = jsonPart.getString("description");
if (main != "" && description != "") {
message += main + ": " + description + "\r\n";
}
}
if (message != "") {
weatherReport.setText(message);
} else {
Toast.makeText(getApplicationContext(), "Could not find weather", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Could not find weather", Toast.LENGTH_LONG).show();
}
}
}
在哪裏放置api密鑰? –
downloadTask.execute(「http://api.openweathermap.org/data/2.5/weather?q=」+ encodedCity +「&APPID = 0a7808bb8061814df9b6d5fc88d58b8f」);我試過這個,但它仍然顯示相同的錯誤..我認爲它必須做一些權限..請回答..因爲我堅持這個問題超過12小時,:( –
你不需要要檢查是否授予'Manifest.permission.INTERNET',它被認爲是[normal](https://developer.android.com/guide/topics/permissions/normal-permissions.html)權限。您只需要確定你將它添加到你的'Manifest.xml'中,我將刪除你的代碼,檢查它是否被授予。 –