Harshal Kalavadiya Harshal Kalavadiya - 4 months ago 22
Java Question

Get method with header parameter not working android

i have following json

{
"category": [{
"id": "90",
"user_id": "1",
"category_id": "27",
"name": "આણંદ કોમર્સિયલ લેયર",
"order": "0",
"created_at": "2014-05-03 17:09:54",
"updated_at": "2014-05-03 17:09:54",
"deleted": "0",
"subtopics": [{
"id": "203",
"user_id": "1",
"category_id": "27",
"subcategory_id": "90",
"name": "આણંદ કોમર્સિયલ લેયર (સંકર જાત)",
"order": "0",
"details": "<p style=\"text-align:justify\"><img alt=\"\" src=\"/packages/wysiwyg/ckeditor/plugins/kcfinder/upload/images/1.png\" style=\"height:271px; width:237px\" /></p>\r\n\r\n<ul>\r\n\t<li style=\"text-align:justify\">પ્રથમ ઈંડું મુક્વાની સરેરાશ ઉંમર:૧૪૨ દિવસ</li>\r\n\t<li style=\"text-align:justify\">સરેરાશ વાર્ષિક ઈંડા ઉત્પાદન : ૩૦૦ ઈંડા</li>\r\n\t<li style=\"text-align:justify\">૪૦ અઠવાડીયાની ઉંમરે ઈંડાનું સરેરાશ વજન : ૫૨ ગ્રામ</li>\r\n\t<li style=\"text-align:justify\">૭૨ અઠવાડીયાની ઉંમરે ઈંડાનું સરેરાશ વજન : ૫૪ ગ્રામ</li>\r\n\t<li style=\"text-align:justify\">સારી જીવાદોરી</li>\r\n</ul>\r\n",
"mobile_detail": "<p style=\"text-align:justify\"><img alt=\"\" src=\"/packages/wysiwyg/ckeditor/plugins/kcfinder/upload/images/1.png\" style=\"height:271px; width:237px\" /></p>\r\n\r\n<ul>\r\n\t<li style=\"text-align:justify\">પ્રથમ ઈંડું મુક્વાની સરેરાશ ઉંમર:૧૪૨ દિવસ</li>\r\n\t<li style=\"text-align:justify\">સરેરાશ વાર્ષિક ઈંડા ઉત્પાદન : ૩૦૦ ઈંડા</li>\r\n\t<li style=\"text-align:justify\">૪૦ અઠવાડીયાની ઉંમરે ઈંડાનું સરેરાશ વજન : ૫૨ ગ્રામ</li>\r\n\t<li style=\"text-align:justify\">૭૨ અઠવાડીયાની ઉંમરે ઈંડાનું સરેરાશ વજન : ૫૪ ગ્રામ</li>\r\n\t<li style=\"text-align:justify\">સારી જીવાદોરી</li>\r\n</ul>\r\n",
"created_at": "2014-05-03 17:11:43",
"updated_at": "2014-05-11 13:41:31",
"deleted": "0",
"images": [],
"videos": []
}]
}]


}

so i set following code

public JSONObject getJSONFromUrl(String url) {

// Making HTTP request
InputStream is=null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
//HttpPost httpPost = new HttpPost(url);
HttpGet httpPost = new HttpGet(url);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("X-Requested-With", "XMLHttpRequest");
httpPost.setHeader("Mobile-Tokon", "7c^4N:9Y*Tq;P^f");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

String json="";
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8000);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}

// try parse the string to a JSON object
JSONObject jObj=null;
try {
jObj = new JSONObject(json);
Log.d("JSON",jObj.toString());
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}

// return JSON String
return jObj;

}


in my Java class i call service like below

getJSONFromUrl("http://ikishan.192.168.1.87.xip.io/api/newapps/27?email=api.ikisan@aau.in&password=%~?7ON9Xjp;BcYu");

Caused by: java.lang.IllegalArgumentException: Invalid % sequence: %~? in query at index 83: http://ikishan.192.168.1.87.xip.io/api/newapps/27?email=api.ikisan@aau.in&password=%~?7ON9Xjp;BcYu


at line
HttpGet httpPost = new HttpGet(url);
any idea how can i solve this ? your all suggestions are appreciable.

Answer

Try like below. if you encrypct password then you can do like below .

// Encrypt Password to UTF-8 format

private String encrypt(String pwd) {
    String encryptedPwd = null;
    try {
        byte[] byteArray = pwd.getBytes("UTF-8");
        encryptedPwd = Base64.encodeToString(byteArray, Base64.DEFAULT).trim();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return encryptedPwd;
}

you can Encode URL with below class

public class EncodingUtil{


 /**
 * Decodes the passed UTF-8 String using an algorithm that's compatible with
 * JavaScript's <code>decodeURIComponent</code> function. Returns
 * <code>null</code> if the String is <code>null</code>.
 *
 * @param s The UTF-8 encoded String to be decoded
 * @return the decoded String
 */
public static String decodeURIComponent(String s)
{
    if (s == null)
    {
        return null;
    }

    String result = null;

    try
    {
        result = URLDecoder.decode(s, "UTF-8");
    }

    // This exception should never occur.
    catch (UnsupportedEncodingException e)
    {
        result = s;
    }

    return result;
}

/**
 * Encodes the passed String as UTF-8 using an algorithm that's compatible
 * with JavaScript's <code>encodeURIComponent</code> function. Returns
 * <code>null</code> if the String is <code>null</code>.
 *
 * @param s The String to be encoded
 * @return the encoded String
 */
public static String encodeURIComponent(String s)
{
    String result = null;

    try
    {
        result = URLEncoder.encode(s, "UTF-8")
                .replaceAll("\\+", "%20")
                .replaceAll("\\%21", "!")
                .replaceAll("\\%27", "'")
                .replaceAll("\\%28", "(")
                .replaceAll("\\%29", ")")
                .replaceAll("\\%7E", "~");
    }

    // This exception should never occur.
    catch (UnsupportedEncodingException e)
    {
        result = s;
    }

    return result;
}

/**
 * Private constructor to prevent this class from being instantiated.
 */
private EncodingUtil()
{
    super();
}}