1

我想通过 Json 将我的 android 应用程序连接到服务器。是否可以通过 github 进行连接我已经提到了这个链接http://developer.github.com/v3/ 但我对此一无所知?谁能解释一下。

4

2 回答 2

0

您还可以使用最新的 Google AsyncHttpClient Api [检查此链接] 或此处的 Volley 。我发现它更简单,更容易用于你想做的事情。例如:

private RequestQueue reqQueue  = Volley.newRequestQueue(this);                              
private JsonObjectRequest jsObjRequest;

public void connectViaVolley(){
String servURL="https://api.github.com/repos/mojombo/jekyll/issues?state=closed"; //this URL is what is on GitHub already, so you can test with it

jsObjRequest = new JsonObjectRequest(Request.Method.GET, servURL, null, new       Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {

//Handle any non-error response and parse the JSONobject
}
 }, new Response.ErrorListener() {

 @Override
 public void onErrorResponse(VolleyError error) {

//handle any errors here [no need for exceptions as Volley does this already]
                }
            });

    //actual network connection/request
 reqQueue.add(jsObjRequest);
    }
于 2013-10-31T06:21:25.587 回答
0

以下方法为您带来来自 URL 的数据。

public static JSONObject getJSONfromURL(String url, Context c) {
            url = url.trim();
            url = url.replace(" ", "%20");
            Log.i("getJSONfromURL", "URL From json functions: " + url);
            InputStream is = null;
            String result = "";
            JSONObject jArray = null;
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);

                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

            } catch (Exception e) {
                Log.d("Json Functions", " The Xceptions are " + e.getMessage());
            }

            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "iso-8859-1"), 8);

                String line = null, name = null;
                while ((line = reader.readLine()) != null) {

                    name = line;
                }
                is.close();
                result = name;

            } catch (Exception e) {

            }

            try {
                if (result != null) {
                    jArray = new JSONObject(result);
                }
            } catch (JSONException e) {

            }
            return jArray;
        }

调用此函数如下:

try {
            JSONObject json = JSONfunctions
                    .getJSONfromURL(Your URL,
                            Yourclassnname.this);
   }catch(Exception e)
{
}

使用 AsyncTask 获取 Json 结果。希望能帮助到你

于 2013-10-28T07:20:03.260 回答