gpt4 book ai didi

java - 提取json对象时出错

转载 作者:行者123 更新时间:2023-12-01 12:32:10 26 4
gpt4 key购买 nike

我必须从 url 的 json 文件中检索 json 对象.

我的代码在 doInBackground() 和字符串到 jsonObject 转换异常中抛出 java.lang.RuntimeException 。有人能帮我吗?我是 Android 编程新手。

package course.examples.networkingearthquake;


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.widget.Button;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.net.http.AndroidHttpClient;

public class HttpActivity extends ActionBarActivity {

TextView mTextView;
EditText etInput;
TextView input;
String number;//edited
int num;//edited
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_socket);

mTextView = (TextView)findViewById(R.id.text1);
input = (TextView)findViewById(R.id.input);
etInput = (EditText)findViewById(R.id.etInput);
input.setText("Input");

//number = etInput.getText().toS();

final Button btDisplay = (Button)findViewById(R.id.btDisplay);
btDisplay.setText("DISPLAY");

btDisplay.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {

new HttpGetTask().execute();

}
});
}


private class HttpGetTask extends AsyncTask<Void, Void, String>{

private static final String TAG = "HttpGetTask";
private static final String URL = "http://earthquake.usgs.gov/earthquakes/feed/geojsonp/2.5/week";

AndroidHttpClient mClient = AndroidHttpClient.newInstance("");

@Override
protected String doInBackground(Void... params){

HttpGet request = new HttpGet(URL);
JSONResponseHandler responseHandler = new JSONResponseHandler();
// ResponseHandler<String> responseHandler = new BasicResponseHandler();

try{

return mClient.execute(request,responseHandler);

}catch(ClientProtocolException exception){
exception.printStackTrace();
}catch(IOException exception){
exception.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String result){
if(null != mClient)
mClient.close();
mTextView.setText(result);
}

}
private class JSONResponseHandler implements ResponseHandler<String>{


@Override
public String handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
String result = null;
String JSONResponse = new BasicResponseHandler().handleResponse(response);
JSONResponse = JSONResponse.substring(17, JSONResponse.length()-3);
num = Integer.parseInt(number);// edited
try {
JSONObject responseObject = (JSONObject) new JSONTokener(
JSONResponse).nextValue();
JSONArray features = responseObject.getJSONArray("features");
JSONObject retObject = (JSONObject)features.get(num);//edited
// JSONObject geometry = (JSONObject)retObject.get("geometry");

result = retObject.toString();
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
}

最佳答案

您指定的 URL 返回的 JSON 包含 eqfeed_callback(),需要将其删除才能使其有效。

似乎您已在响应处理程序中完成了此操作,但您在开始和结束时都过多地切断了一个字符。

试试这个:

JSONResponse = JSONResponse.substring(16, JSONResponse.length()-2);

关于java - 提取json对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25852640/

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