Skip to content

Instantly share code, notes, and snippets.

@lithid
Last active December 27, 2015 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lithid/7327067 to your computer and use it in GitHub Desktop.
Save lithid/7327067 to your computer and use it in GitHub Desktop.
json
public String getNetFile(String device, String url, boolean isJ) {
DefaultHttpClient defaultClient = new DefaultHttpClient();
String urlStr = url;
HttpGet httpGetRequest = new HttpGet(urlStr);
HttpResponse httpResponse = null;
try {
httpResponse = defaultClient.execute(httpGetRequest);
} catch (IllegalStateException e) {
return null;
} catch (IllegalArgumentException e) {
return null;
} catch (ClientProtocolException e) {
return null;
} catch (IOException e) {
return null;
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(httpResponse
.getEntity().getContent(), "UTF-8"));
} catch (NullPointerException e) {
return null;
} catch (UnsupportedEncodingException e) {
return null;
} catch (IllegalStateException e) {
return null;
} catch (IOException e) {
return null;
}
String json = null;
StringBuilder total = new StringBuilder();
try {
while ((json = reader.readLine()) != null) {
if (json.equals("<!doctype html>")) {
return null;
}
total.append(json + "\n");
}
} catch (NullPointerException e) {
return null;
} catch (IOException e) {
return null;
}
return total.toString();
}
class loadAllUpdates extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
setProgressBarIndeterminateVisibility(true);
}
@Override
protected String doInBackground(String... arg0) {
String json = mUtils.getNetFile(url);
try {
JSONObject jsonObject = new JSONObject(json);
mArray = jsonObject.getJSONArray("updates");
int success = jsonObject.getInt("success");
mRom = jsonObject.get("_name").toString();
switch (success) {
case 1:
for (int i = 0; i < mArray.length();) {
JSONObject c = mArray.getJSONObject(i);
mId = c.getlong("_ID");
mFile = c.getString("_FILE");
mMd5sum = c.getString("_MD5SUM");
mVersion = c.getString("_VERSION");
mChangelog = c.getString("_CHANGELOG");
mDate = c.getint("_DATE");
return mId;
}
return json;
case 0:
return null;
}
} catch (NullPointerException e) {
return null;
} catch (JSONException e) {
return null;
}
return json;
}
@Override
protected void onPostExecute(final String string) {
setProgressBarIndeterminateVisibility(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment