任何人都可以告诉我如何在指定的URL中读取此JSON

时间:2014-05-08 05:48:48

标签: android json

我一直在这个URL中读取这个json文件。我在网上尝试了很多例子,但它无法在此URL中读取此特定的json文件

[https://203.177.52.163:1985/restgateway/services/ws_download/Book/3372/Plant/03/User/12334]. Please help me what is wrong with this. Thank you for your help.

//这是我的代码示例..



        package com.hmkcode.android;

        import java.io.BufferedReader;
        import java.io.IOException;
        import java.io.InputStream;
        import java.io.InputStreamReader;
        import org.apache.http.HttpResponse;
        import org.apache.http.client.HttpClient;
        import org.apache.http.client.methods.HttpGet;
        import org.apache.http.impl.client.DefaultHttpClient;
        import android.net.ConnectivityManager;
        import android.net.NetworkInfo;
        import android.os.AsyncTask;
        import android.os.Bundle;
        import android.util.Log;
        import android.widget.EditText;
        import android.widget.TextView;
        import android.widget.Toast;
        import android.app.Activity;

        public class MainActivity extends Activity {

            EditText etResponse;
            TextView tvIsConnected;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // get reference to the views
        etResponse = (EditText) findViewById(R.id.etResponse);
        tvIsConnected = (TextView) findViewById(R.id.tvIsConnected);

        // check if you are connected or not
        if(isConnected()){
            tvIsConnected.setBackgroundColor(0xFF00CC00);
            tvIsConnected.setText("You are conncted");
        }
        else{
            tvIsConnected.setText("You are NOT conncted");
        }

        // call AsynTask to perform network operation on separate thread
        //new HttpAsyncTask().execute("http://hmkcode.appspot.com/rest/controller  /get.json");
      new HttpAsyncTask().execute("https://203.177.52.163:1985/restgateway/services/ws_download/Book/3372/Plant/03/User/12334");

    }

       public static String GET(String url){
        InputStream inputStream = null;
        String result = "";
        try {

            // create HttpClient
            HttpClient httpclient = new DefaultHttpClient();

            // make GET request to the given URL
            HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

            // receive response as inputStream
            inputStream = httpResponse.getEntity().getContent();

            // convert inputstream to string
            if(inputStream != null)
                result = convertInputStreamToString(inputStream);
            else
                result = "Did not work!";

        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }

        return result;
    }

     private static String convertInputStreamToString(InputStream inputStream) throws IOException{
        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
        String line = "";
        String result = "";
        while((line = bufferedReader.readLine()) != null)
            result += line;

        inputStream.close();
        return result;

    }

    public boolean isConnected(){
        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected())
                return true;
            else
                return false;  
    }
    private class HttpAsyncTask extends AsyncTask {
        @Override
        protected String doInBackground(String... urls) {

            return GET(urls[0]);
        }
        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(String result) {enter code here
            Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
            etResponse.setText(result);
          }
       }
    }

这是我的activity_main.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

   <TextView
        android:id="@+id/tvIsConnected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"    
        android:background="#FF0000"
        android:textColor="#FFF"
        android:textSize="18dp"
        android:layout_marginBottom="5dp"
        android:text="is connected? " />

    <EditText
        android:id="@+id/etResponse"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:ems="10"
        android:layout_marginTop="10dp"
        android:inputType="textMultiLine" >\
        <requestFocus />
    </EditText>
</LinearLayout>

此代码可以使用此URL

new HttpAsyncTask().execute("http://hmkcode.appspot.com/rest/controller  /get.json");

但如果我将其更改为此网址

new HttpAsyncTask().execute("https://203.177.52.163:1985/restgateway/services/ws_download/Book/3372/Plant/03/User/12334");

伙计们,我想知道这是否是服务器问题?或者这只是这段代码的错误。提前谢谢。

1 个答案:

答案 0 :(得分:1)

我的猜测是您尝试连接到自签名的HTTPS网站。这意味着您的证书不正确&#34; (由知名公司认证)。因此,android可能会阻止此请求,例如&#34; SSL peer certificate incorect&#34;或者其他什么。

如果我的猜测是正确的,我会将您重定向到这个问题:Self-signed certificate and loopj for Android以及接受的答案对我来说是非常正确的。

尝试并告诉我它是否有帮助!