从XML字符串中提取jsonarray



我试图通过使用asynctask通过http post方法调用url链接。如果链接是"http://example.com/broadcast/",我正在添加"name=ravi/age=30/"参数,就像这样。操作正在工作,因为获得了输出,但我得到的响应是xml字符串格式,其中包含JSONArray。我只是想从xml字符串中提取JSONArray输出。

webResponse output:
<?xml version="1.0"  
encoding="utf-8"?>
<string xmlns="https://example.com/statement">
[{"status":"ok","code":"pass"}]</string>
public class HttpPostExample extends Activity {
	
	
    /** Called when the activity is first created. */ 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http_post_example);
        
    Button saveme=(Button)findViewById(R.id.save);
    saveme.setOnClickListener(new Button.OnClickListener(){
        	public void onClick(View v){
        		
        			
        		new AsyncCallWS().execute();
        		
        	}
     });  
  } //oncreate
    
private class AsyncCallWS extends AsyncTask<String, Void, String> {
    	
    	
    	
		@Override
        protected void onPreExecute() {
            
    		
        }
		@Override
		protected String doInBackground(String... args) {
			// TODO Auto-generated method stub
			String webResponse=""; 
			
			HttpClient httpClient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost("http://example.com/going.asmx/broadcast?"); 
			//Post Data
			List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(5);
			nameValuePair.add(new BasicNameValuePair("alpha", "A1B2C3"));
			nameValuePair.add(new BasicNameValuePair("type", "IN"));
			nameValuePair.add(new BasicNameValuePair("name", "Somnath"));
			nameValuePair.add(new BasicNameValuePair("bind", "Hour Glass"));
			nameValuePair.add(new BasicNameValuePair("body", "Hundo"));
			//Encoding POST data
			try{
					httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
			}catch (UnsupportedEncodingException e) {
	
				e.printStackTrace();
				System.out.println(e);
			}
			//making POST request.
			try{
					HttpResponse response = httpClient.execute(httpPost);
					webResponse = EntityUtils.toString(response.getEntity());
				
					
					
			}catch (ClientProtocolException e) {
				// Log exception
				e.printStackTrace();
				System.out.println(e);
			} catch (IOException e) {
				// Log exception
				e.printStackTrace();
				System.out.println(e);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		     
			return  webResponse;
		}
		
			@Override
	    protected void onPostExecute(String webResponse) {
	         
			   
				Toast.makeText(getApplicationContext(), webResponse, Toast.LENGTH_LONG).show();
	    }
	     
    } // AsyncTask ends
    
    
	
} //activity

尝试通过下面的代码删除xml代码,并从postExecute中获取json数组:

    int first,second;
    first = webResponse.indexOf("[");
    second = webResponse.indexOf(first +1 , webResponse.length());
    String json = webResponse.substring(first, second);
    JSONObject jsonObj = new JSONObject(json);
    JSONArray array = jsonObj.getJSONArray("status");

我使用下面的代码得到了所需的结果:

   HttpResponse response = httpClient.execute(httpPost);
				String XmlString = EntityUtils.toString(response.getEntity());
				XmlString=XmlString.replaceAll("\<\?xml(.+?)\?\>", "").trim();
				XmlString = XmlString.substring(XmlString.indexOf("[") + 1, XmlString.lastIndexOf("]"));
				JSONObject jObj = new JSONObject(XmlString);
				webResponse = jObj.getString("status");

最新更新