如何在android应用程序中使用github?
我想通过Json将我的android应用程序连接到服务器。有没有可能通过github进行连接?我引用了这个链接,但我对此一无所知。有人能解释一下吗。以下方法可以从URL获取数据如何在android应用程序中使用github?,android,json,github,Android,Json,Github,我想通过Json将我的android应用程序连接到服务器。有没有可能通过github进行连接?我引用了这个链接,但我对此一无所知。有人能解释一下吗。以下方法可以从URL获取数据 public static JSONObject getJSONfromURL(String url, Context c) { url = url.trim(); url = url.replace(" ", "%20"); Log.i("getJ
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结果。希望它有帮助您也可以使用Volley,最新的Google AsyncHttpClient Api[检查此项]或。我发现它更简单,更容易用于你想做的事情。例如:
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);
}
private RequestQueue reqQueue=Volley.newRequestQueue(this);
私有JSONObject请求jsObjRequest;
公共排球{
字符串servURL=”https://api.github.com/repos/mojombo/jekyll/issues?state=closed“;//此URL是GitHub上已经存在的URL,因此您可以使用它进行测试
jsObjRequest=newjsonobjectrequest(Request.Method.GET,servURL,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
//处理任何非错误响应并解析JSONobject
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
//在此处理任何错误[不需要异常,因为Volley已经这样做了]
}
});
//实际网络连接/请求
添加(jsObjRequest);
}
如何在github中构造url?