【发布时间】:2016-08-04 01:54:02
【问题描述】:
我正在尝试从服务器获取数据并将其显示在 ListView 中,但它在这里不起作用,我正在使用 Volley 库。我正在找Volley的代码,但是我找不到,请问我可以得到这个对应的代码吗?
JSONParse.java:
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.client.HttpClient;
import org.json.JSONException;
public class JSONParse {
static InputStream iStream=null;
static JSONArray jarray=null;
static String json="";
public JSONParse(){
}
public JSONArray getJSONFromUrl(String url){
StringBuilder builder=new StringBuilder();
HttpClient client=new DefaultHttpClient();
//------------J.G-----------------
/*HttpClient client=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(url);
httpGet.setHeader("Content-Type", "application/json");
httpGet.setHeader("jwttoken", "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJFUk0iLCJpc3MiOiJFUk0iLCJpYXQiOjE0MzM2OTczMjd9.2zd4OlKjW3Yfcd_q2FOoyEGpNrFbf7EuHeIkZ8ponr0");*/
try {
HttpGet httpGet=new HttpGet(url);
httpGet.setHeader("Content-Type", "application/json");
httpGet.setHeader("jwttoken", "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJFUk0iLCJpc3MiOiJFUk0iLCJpYXQiOjE0MzM2OTczMjd9.2 zd4OlKjW3Yfcd_q2FOoyEGpNrFbf7EuHeIkZ8ponr0");
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e("==>", "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
jarray= new JSONArray(builder.toString());
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data"+ e.toString());
}
return jarray;
}
}
【问题讨论】:
-
不,你不会得到代码,你需要自己尝试。
标签: java android json android-volley