0

I have the following code to connect from my android application to zappos api server and search for some stuff. But It either returns error 404 or We are unable to process the request from the input feilds given.

When I execute the same query it works on the web browser.

The query is:

http://api.zappos.com/Search&term=boots&key=<my_key_inserted_here>

Code:

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://api.zappos.com/Search");

NameValuePair keypair = new BasicNameValuePair("key",KEY);
NameValuePair termpair = new BasicNameValuePair("term",data);

List<NameValuePair> params = new ArrayList<NameValuePair>(2);

params.add(keypair);
params.add(termpair);

post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(post);

String str;
StringBuilder sb = new StringBuilder();
HttpEntity entity =response.getEntity();
if (entity != null) {
    DataInputStream in = new DataInputStream(entity.getContent());
    while (( str = in.readLine()) != null){
        sb.append(str);
    }

    in.close();
}

Log.i("serverInterface","response from server is :"+sb.toString());

What am I doing wrong?

n1k1ch
  • 2,594
  • 3
  • 31
  • 35
  • I would begin by looking through the docs for Commons-HttpClient and figure out a way to make sure the request that is actually being made is what you're expecting. – Mike B Feb 26 '14 at 21:34
  • 1. Are You sending a `POST` request when API server expects `GET`? 2. Maybe the API server expects the client to set `Accept: application/json` header or some other headers? – saabeilin Feb 26 '14 at 21:37
  • if it helps, the documentation page at http://developer.zappos.com/ says it is a REST api. I don't know what that means. Can you explain what it is? I don't think GET works either – Prasana Venkat Ramesh Feb 27 '14 at 00:59

2 Answers2

1

If I am correct, what you want to do is a GET request with parameters.

Then,the code would looks like something like that:

    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet("http://api.zappos.com/Search");

    HttpParams params = new BasicHttpParams();
    params.setParameter("key", "KEY");
    params.setParameter("term", "data");
    get.setParams(params);

    HttpResponse response;
    response = client.execute(get);

    String str;
    StringBuilder sb = new StringBuilder();
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        DataInputStream in;
        in = new DataInputStream(entity.getContent());
        while ((str = in.readLine()) != null) {
            sb.append(str);
        }
        in.close();
    }

    Log.i("serverInterface", "response from server is :" + sb.toString());
Elodie E2
  • 580
  • 3
  • 10
  • Hi No luck. The message as shown in the response from the server is : access is denied. Status Code 403 – Prasana Venkat Ramesh Feb 27 '14 at 00:58
  • It's also what I get when I test the url you provided : http://api.zappos.com/Search&term=boots&key= Are you sure that your key is correct? – Elodie E2 Feb 27 '14 at 01:15
  • Yeah. It was generated by the recruiters at zappos. This is a common interview question. It works now. Please see the answer I posted. And thanks a lot for your help. – Prasana Venkat Ramesh Feb 27 '14 at 01:21
0

I found an answer to the question based on ALL of your help. I got the hint that I must search how to connect to REST service and I also used this result. This is the exact result I was looking for. Sadly it resembles too much to what I'm trying to achieve that I think whoever asked it might be applying to the same position :(

Community
  • 1
  • 1