- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已按照以下教程进行操作,但我的代码出现系统错误: http://www.informit.com/articles/article.aspx?p=2154675&seqNum=4
以下是所有错误:
W/EGL_emulation: eglSurfaceAttrib not implementedW/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f651f4e3780, error=EGL_SUCCESSW/DefaultRequestDirector: Authentication error: Unable to respond to any of these challenges: {}W/System.err: org.json.JSONException: End of input at character 0 of W/System.err: at org.json.JSONTokener.syntaxError(JSONTokener.java:450)W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:97)W/System.err: at org.json.JSONObject.(JSONObject.java:156)W/System.err: at org.json.JSONObject.(JSONObject.java:173)W/System.err: at com.example.theol.opsc7312_assign2_14001515_theolin_naidoo.MainActivity$ReadJSONFeed.onPostExecute(MainActivity.java:74)W/System.err: at com.example.theol.opsc7312_assign2_14001515_theolin_naidoo.MainActivity$ReadJSONFeed.onPostExecute(MainActivity.java:46)W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:636)W/System.err: at android.os.AsyncTask.access$500(AsyncTask.java:177)W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)W/System.err: at android.os.Looper.loop(Looper.java:135)W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)W/System.err: at java.lang.reflect.Method.invoke(Native Method)W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)Application terminated.
Here's my code:
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import org.apache.http.client.methods.HttpPost;
import org.json.JSONException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.HttpClient;
import org.apache.http.HttpResponse;
import org.apache.http.HttpEntity;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import android.os.AsyncTask;
import org.json.JSONObject;
import org.apache.http.StatusLine;
import org.json.JSONArray;
import java.text.NumberFormat;
public class MainActivity extends Activity {
String city="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button submitButton = (Button)this.findViewById(R.id.submit_btn);
submitButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
String country="";
EditText cityName = (EditText) findViewById(R.id.city_name);
city=cityName.getText().toString();
String url="http://api.openweathermap.org/data/2.5/weather?q="+city;
new ReadJSONFeed().execute(url);
}
});
}
private class ReadJSONFeed extends AsyncTask<String, String, String> {
protected void onPreExecute() {}
@Override
protected String doInBackground(String... urls) {
HttpClient httpclient = new DefaultHttpClient();
StringBuilder builder = new StringBuilder();
HttpPost httppost = new HttpPost(urls[0]);
try {
HttpResponse response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
protected void onPostExecute(String result) {
String weatherInfo="Weather Report of "+city +" is: \n";
try{
JSONObject jsonObject = new JSONObject(result);
JSONObject jscoordObject = new JSONObject(jsonObject.getString("coord"));
weatherInfo+="Longitude: "+jscoordObject.getString("lon")+"\n";
weatherInfo+="Latitude: "+jscoordObject.getString("lat")+"\n";
JSONArray jsweatherObject = new JSONArray(jsonObject.getString("weather"));
JSONObject jweatherObject = jsweatherObject.getJSONObject(0);
weatherInfo+="Clouds: "+jweatherObject.getString("description")+"\n";
JSONObject jsmainObject = new JSONObject(jsonObject.getString("main"));
weatherInfo+="Humidity: "+jsmainObject.getString("humidity")+"% \n";
weatherInfo+="Atmospheric Pressure: "+jsmainObject.getString("pressure")+"hPa \n";
float temp=Float.parseFloat(jsmainObject.getString("temp"));
temp = temp - (float) 273.15;
NumberFormat df = NumberFormat.getNumberInstance();
df.setMaximumFractionDigits(2);
weatherInfo+="Temperature: "+ String.valueOf(df.format(temp)) +" C\n";
JSONObject jswindObject = new JSONObject(jsonObject.getString("wind"));
weatherInfo+="Wind Speed: "+jswindObject.getString("speed")+"mps \n";
}
catch (JSONException e) {
e.printStackTrace();
}
TextView resp = (TextView) findViewById(R.id.response);
if(weatherInfo.trim().length() >0 )
resp.setText(weatherInfo);
else
resp.setText("Sorry no match found");
}
}
}
最佳答案
根据错误,您没有收到 JSON,这可能意味着您收到了一些 HTTP 错误。
如果在尝试解析结果
之前记录
结果的值,您可以更容易地看到这一点。
但是,您应该意识到这一点。
"To access the API you need to sign up for an API key if you are on a free or paid plan."
- http://openweathermap.org/api
请参阅此处 use the API key
换句话说,获取 API key ,然后将其附加到您的 URL。例如
String API_KEY = "XXXXxxxx";
String url = "http://.../weather?q=" + city + "&APPID=" + API_KEY;
关于java - org.json.JSONException : End of input at character 0 of,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39899921/
我有 7 个协议(protocol),我想在网站上显示。但该网站仅显示 4 个协议(protocol),尽管 JSON 文件中有 7 个协议(protocol)。我决定用调试器检查一下,它通过了 4
我在此代码中遇到 JSON 异常: JSONObject jObject = new JSONObject(JSONString); pontosUsuario.setIdUsuari
我创建了以下价格和移动型号名称的 json 数组。此数据是从表名称为“products”的 wamp phpmyadmin 服务器读取的: { "products": [
我正在从 linkedHashMap 获取 JSON 值 LinkedHashMap lst = JsonPath.parse(json).read("$.projects[0].issuetypes
我正在尝试使用来自 Guardian API 的 JSON 对象。 这是我调用的结果: https://pastebin.com/wqggLEeZ 这是我的代码 JSONObject root = n
我正在尝试从数据库中检索数据。我向此 PHP 文件发送请求: $code, "message"=>$message); echo json_encode($err); }
JSONObject login = new JSONObject(); login.put("Key1", "Value1"); 我只是想用键值对创建一个简单的 JSON 对象。我收到此异常“Unh
我试图解析下面的 json 并在第 150 行(在代码中标记)捕获了 JSONException,但我无法弄清楚为什么会发生。目标是找到特定包的测试列表并将其存储在 arraylist 中。 当我将
我在解析 JSON 时遇到很多问题,其中一个值具有空值。 { "available_from" : "2012-11-05T00:00:00Z", "available_to" : "2012-11-
我有一个带有一些 spring crud 存储库的经典 spring web 应用程序。 我正在尝试以经典的 Angular 形式保存我的实体,但我随机收到此错误: .w.s.m.s.DefaultH
我正在尝试将字符串转换为 JSONObject。这是我的代码: JSONObject obj = new JSONObject(str); Vehicle.feature 文件包含: Scen
@Override protected Integer doInBackground(Void... params) { String readMyJSON = rea
我想解析一些从网上下载的 JSONArray 中的 json。我认为这很简单,但无法让它发挥作用。 看来问题出在 JSON 格式上。我尝试了不同的方法修复它,但没有任何帮助。 我用这种方法下载字符串:
我收到以下错误: JSONException: No value for error 在 Android Studio 中的我的日志(查看日志截图的链接: /image/45eeF.png )上,它说
它可以很好地从数据库读取和写入,但会给出 JSONException 错误。任何帮助将不胜感激! 谢谢 最佳答案 此错误发生在 JSONObject 的 getter 函数中,这意味着您尝试使用 JS
我想从此链接获取json数据:link &这是我的代码: private static String url = "https://graph.facebook.com/fql?q=SELECT%20
描述:在 OnRespose 方法中,我可以在日志中看到从 froecast.io 接收的数据,但是当我传递“response.body().string()”时,即来自 Forecast.io 的数
我正在尝试从此网址中抓取书名:http://api.nytimes.com/svc/books/v2/lists/2010-10-01/trade-fiction-paperback?api-key=
我一直在尝试在 android studio 中为我的应用程序创建一个登录系统,但我不断收到此错误 org.json.JSONException: Value Connection of type j
我有一个 iOS 应用程序和一个 Android 应用程序,可以使用 RESTFul Web 服务。 当我向服务进行查询时,我得到一个像这样返回的 JSON 对象。 [ { "
我是一名优秀的程序员,十分优秀!