如何在android应用程序中使用github?

时间:2013-10-28 07:08:22

标签: android json github

我想通过Json将我的android应用程序连接到服务器。是否有可能通过github连接我已经引用了这个链接http://developer.github.com/v3/但是我对此没有任何想法?可以解释一下。

2 个答案:

答案 0 :(得分: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结果。希望它有所帮助

答案 1 :(得分:0)

您还可以使用Volley,最新的Google AsyncHttpClient Api [查看此link]或here。我发现它更简单易用,适合你想做的事情。例如:

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);
    }