提取json对象时出错

时间:2014-09-15 16:29:54

标签: java android json

我必须从这个url的json文件中检索json对象。

我的代码在doInBackground()和string到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;
    }
  }

1 个答案:

答案 0 :(得分:1)

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

看起来你已经在你的响应处理程序中完成了这个,但是你在开始和结束时都过多地切断了一个字符。

试试这个:

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