• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem in HttpPOST/GET

Howdy,

I am trying to login in a website via http post and then via http get download a json file (.do file with json content). This json file url contains a group of parameters that I already sent on the url. Although, I can successfully login I cannot access the content from the json file - I'm downloading the html code of the site - i have already tried for an image or a css and it works.


The code goes like this:

Code:
HttpClient client = new DefaultHttpClient();    
HttpPost req_post = new HttpPost(url_post);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//...
HttpGet req_get = new HttpGet(url_get);
HttpResponse resp = client.execute(req_get);
Can someone give me a hint?

Thanks in advance.
 
Give this a shot...

JSONfunctions
Code:
public class JSONfunctions {

	public static JSONObject getJSONfromURL(String url){
		InputStream is = null;
		String result = "";
		JSONObject jArray = null;
		
		//http post
	    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.e(TAG, "Error in http connection "+e.toString());
	    }
	    
	  //convert response to string
	    try{
	            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
	            StringBuilder sb = new StringBuilder();
	            String line = null;
	            while ((line = reader.readLine()) != null) {
	                    sb.append(line + "\n");
	            }
	            is.close();
	            result=sb.toString();
	    }catch(Exception e){
	            Log.e(TAG, "Error converting result "+e.toString());
	    }
	    
	    try{
	    	
            jArray = new JSONObject(result);            
	    }catch(JSONException e){
	            Log.e("log_tag", "Error parsing data "+e.toString());
	    }
    
	    return jArray;
	}
}

You can use the above class like the following:


Code:
JSONObject json = JSONfunctions.getJSONfromURL(params);

Then process the json by passing the object to the following function (may be the only true thing you want to reference):

Code:
public void processJSON(JSONObject obj) {
        try
            {
                Log.d(TAG, "Start JSON Process...");

                JSONArray serverArray = obj.getJSONArray("server_array");

                for( int i = 0 ; i < serverArrary.length() ; ++i ) {

                        JSONObject arrayObject = serverArray[i].getJSONObject(n);

                        if (! arrayObject.isNull("id")) {
                                  mInt =  arrayObject.getInt("id"));
                        }

                        if (! arrayObject.isNull("name")) {
                                  mString = arrayObject.getString("name"));
                        }

                }

                Log.d(TAG, "END JSON Process...");

            } catch (JSONException e) {
                e.printStackTrace();
            }
}

On top of this, you want to verify that you are sending the proper response back. The best tip I can say is to get a browser plugin that formats JSON and then you can manually enter the URL and see exactly the response you are getting back. If you still need some help after giving this a try... let me know your troubles and I'll follow up.
 
Hi, If use that code my authentication in the website fails. If i use the same httpclient that I've used to make the post authentication it also doesn't retrieve the currect file that i want.

Another idea?
 
Edit, I'm making some test requests and as far as I've noticed I think the problem is due to the parameters I'm sending. In other words, is it possible that the problem is in my url?

I'm doing like this:

Code:
String url = "https://weblink.com/website.do/?method=actualizaDados&_=1318119748567&start=1317596400&end=1318201200";
HttpGet req = new HttpGet(url);
HttpResponse resp = client.execute(req);
 
The url could be causing the issue. My example was just to show you another way you could set it up... but your code should work fine to send the request.. then you can just get back the response and use some of the code I posted.

The important thing here is to make sure you URL encode characters like in the folowing:


Code:
StringBuffer params = new StringBuffer();
        params.append(REQUEST_API).append("?");
        params.append("id=").append(id);
        params.append("name=").append(URLEncoder.encode(name));
 
Thanks, after several hours hitting the wall I've found that it was a problem in the site itself. I had to make another request before I could retrieve the file that I wanted.

Thanks ;)
 
Back
Top Bottom