【发布时间】:2014-04-02 17:46:37
【问题描述】:
已解决非常感谢大家
Helle,首先感谢您点击我的问题。我制作了一个应用程序来获取天气的 json 文件。然后它解析这个 JSON 文件以获取天气变量,如温度等。它在 textView 中显示这些变量。问题是当我按下按钮(在我的模拟器上)时,它显示“不幸的是,(应用程序名称)已停止工作”。下面你可以找到我用来做这一切的代码。帮助将不胜感激。如果您需要更多文件,如 activity_main.xml、strings.xml 等,请询问。
感谢您的帮助感谢大家的帮助,我认为还有 3 个错误需要解决,但我不明白这些错误日志,我刚刚开始编码,谢谢大家的帮助(您可以在帖子底部找到他们
MainActivity.java:
package com.example.thelexapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Random;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
try {
getURLforweather();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
if (location != null) {
TextView text = (TextView) findViewById(R.id.textView1);
text.setTextColor(Color.parseColor("#808080"));
double longitude = location.getLongitude();
double latitude = location.getLatitude();
String longitudestring = String.valueOf(longitude);
String latitudestring = String.valueOf(latitude);
String url = "http://api.worldweatheronline.com/free/v1/weather.ashx?q="
+ longitudestring
+ ","
+ latitudestring
+ "&format=json&num_of_days=1&key=hv3rd4u49qdmf6q3hp5apy6b";
parseJson(url);
String temp_C = null;
String windspeedKmph = null;
String weatherDesc = null;
TCWSKMPH(temp_C, windspeedKmph);
WD(weatherDesc);
}
}
public void parseJson (String url) {
DefainputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
in
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
storeJSONVariables(result);
}
public void
try {
JSONObject oneObject = jArrayCurrent.getJSONObject(i);
JSONArray jArrayWeatherDesc = jObject.getJSONArray("weatherDesc");
String temp_C = oneObject.getString("temp_C");
String windspeedKmph = oneObject.getString("windspeedKmph");
// Pulling items from the array
String weatherDesc = twoObject.getString("value");
WD(weatherDesc);
} catch (JSONException e) {
}
public void TCWSKMPH(String temp_C, String windspeedKmph) {
if(temp_C != null || windspeedKmph != null) {
RwindspeedKmph.nextInt(max - min + 1) + min;
temp_C = getString(a);
windspeedKmph = getString(b);
}
TextView temptext = (TextView)findViewById(R.id.temperature);
TextView sowtext = (TextView)findViewById(R.id.windspeed);
temptext.setText("TEMPERATURE: " + temp_C);
sowtext.setText("WINDSPEED: " + windspeedKmph);
}
public void WD(String weatherDesc) {
String descriptionofweather = weatherDesc;
LogCat:
03-01 11:57:40.760: E/AndroidRuntime(2471): at com.example.thelexapp.MainActivity.TCWSKMPH(MainActivity.java:190)
03-01 11:57:40.760: E/AndroidRuntime(2471): at com.example.thelexapp.MainActivity.getURLforweather(MainActivity.java:92)
03-01 11:57:40.760: E/AndroidRuntime(2471): at com.example.thelexapp.MainActivity$1.onClick(MainActivity.java:42)
【问题讨论】:
-
你在哪里调用了这个 getPositionAndGetWheater() 方法?并发布您的 logcat 以便更好地理解
-
在应用崩溃时查看堆栈跟踪以了解导致问题的原因会很有帮助。
-
嗯,从来没有注意到我无处使用它,但是如果我按下按钮,它会运行 getweather(我认为)导致文本更改为“无法获取天气信息”(因为我没有不知道如何在模拟器中使用 GPS,所以返回 null)
-
@user3357521 尝试进入真实设备,但您的
Buttononclick(...)事件在哪里?你是一个真正的火箭程序员。 -
我没有真正的设备,我正在为朋友制作这个应用程序,我有一部 iphone:s
标签: android xml json parsing weather