Java 使用asp.net进行Android JSON解析
我想在android中解析JSON。问题是,我的web服务是用Asp.net编写的。当用php编写web服务时,我尝试获取JSON数据。但我没有得到结果。我在下面附上我的代码。 用于http处理的WebRequest类Java 使用asp.net进行Android JSON解析,java,android,asp.net,json,Java,Android,Asp.net,Json,我想在android中解析JSON。问题是,我的web服务是用Asp.net编写的。当用php编写web服务时,我尝试获取JSON数据。但我没有得到结果。我在下面附上我的代码。 用于http处理的WebRequest类 public class WebRequest { public static String getData(String url){ StringBuffer response = new StringBuffer();
public class WebRequest {
public static String getData(String url){
StringBuffer response = new StringBuffer();
URL obj = null;
try {
System.out.println("URL :: " + url);
obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
int responseCode = con.getResponseCode();
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString());
} else {
System.out.println("GET request not worked");
}
} catch (UnsupportedEncodingException e) {
Log.d("login issue", e.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response.toString();
}
public static String postData(String data,String url){
StringBuffer response = new StringBuffer();
URL obj = null;
try {
obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
// For POST only - START
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(data.getBytes("UTF-8"));
os.flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString());
} else {
System.out.println("POST request not worked");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response.toString();
}
}
包含异步任务的主要活动
public class MainActivity extends AppCompatActivity {
ImageView im;
Button b1;
TextView t1;
private ProgressDialog pDialog;
String img_url;
JSONArray ja=null;
String s;
String url="http://qutofv2.anathothonline.us/json.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
im= (ImageView) findViewById(R.id.imageView1);
b1= (Button) findViewById(R.id.button);
t1= (TextView) findViewById(R.id.textView1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new getImage().execute(url);
}
});
}
private class getImage extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading...Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
try{
s=WebRequest.getData(params[0]);
}catch (Exception e){
}
return s;
}
@Override
protected void onPostExecute(String file_url) {
pDialog.dismiss();
try{
JSONObject jo=new JSONObject(file_url);
ja=jo.getJSONArray("Classifieds");
JSONObject c=ja.getJSONObject(0);
img_url=c.getString("url");
Toast.makeText(getApplicationContext(),""+file_url,Toast.LENGTH_LONG).show();
}catch(JSONException e){
e.printStackTrace();
}
t1.setText(img_url);
}
}
}
public类MainActivity扩展了AppCompatActivity{
ImageView im;
按钮b1;
文本视图t1;
私人对话;
字符串img_url;
JSONArray ja=null;
字符串s;
字符串url=”http://qutofv2.anathothonline.us/json.php";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
im=(ImageView)findViewById(R.id.imageView1);
b1=(按钮)findViewById(R.id.Button);
t1=(TextView)findViewById(R.id.textView1);
b1.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
新建getImage().execute(url);
}
});
}
私有类getImage扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(MainActivity.this);
setMessage(“正在加载…请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
s=WebRequest.getData(参数[0]);
}捕获(例外e){
}
返回s;
}
@凌驾
受保护的void onPostExecute(字符串文件\u url){
pDialog.disclose();
试一试{
JSONObject jo=新的JSONObject(文件url);
ja=jo.getJSONArray(“分类广告”);
JSONObject c=ja.getJSONObject(0);
img_url=c.getString(“url”);
Toast.makeText(getApplicationContext(),“”+文件\u url,Toast.LENGTH\u LONG.show();
}捕获(JSONException e){
e、 printStackTrace();
}
t1.setText(img_url);
}
}
}
任何人都可以帮忙……您发布了大量代码,但没有说明问题是什么或发生在哪里。请阅读。代码中没有错误。结果是得不到回报。这是唯一的问题。我不知道“结果没有得到”是什么意思。你期望发生什么,发生了什么?你的url扩展名是.php,你说你正在调用.NETWebService??奇怪@ThomasMKurialassery。首先检查您是否收到来自url的任何响应,使用浏览器中的rest postman客户端。传递url和参数,并验证响应。