Monday, May 28, 2012

Json request and Response


Json request without parameters 

InputStream is = null;
public String postData(String url)
{


// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);

try {

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
// 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("log_tag", "Error converting result " + e.toString());
}
//  response in  string variable in json format
return result;

}

Json request with parameters 



public void Uplode(String url1, boolean f, String guid, boolean playnow)
throws JSONException {

try {

JSONObject json = new JSONObject();


json.put("guid", guid);
json.put("description", "this video file is send by the mobile");
        json.put("preempt", playnow);
json.put("front", f);

DataOutputStream wr;
String responseHeader;

HttpURLConnection connection;

URL url = new URL("http://flingo.tv/fling/fling");

                        // Transmit the request document
connection = (HttpURLConnection) url.openConnection();

System.out.println("url conn" + connection);

connection.setDoOutput(true);

connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");

connection.connect();

// Send request
wr = new DataOutputStream(connection.getOutputStream());
wr.write(json.toString(1).getBytes("UTF8"));
wr.flush();
wr.close();

// Get Response
System.out.println("errroo code" + connection + "coonectiom"
+ connection.getResponseCode());

responseHeader = connection.getHeaderField(0);



if (responseHeader.contains("200")) {

System.out.println("result.........."
+ responseHeader.toString());

}

}

catch (Exception e) {
e.printStackTrace();

}

}






Response in (Json) Parsing 


private void parse(String jString) throws Exception {

JSONObject jObject = new JSONObject(jString);

String attricount = jObject.getString("count");
System.out.println("count=" + attricount);

String attriyourip = jObject.getString("yourip");
System.out.println("yourip=" + attriyourip);

String attriinterval = jObject.getString("interval");
System.out.println("interval=" + attriinterval);

JSONArray deviceArray = jObject.getJSONArray("devices");

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


System.out.println("device_model_id="
+ deviceArray.getJSONObject(i).getString("model_id")
.toString());

device_guid_array_list.add(deviceArray.getJSONObject(i)
.getString("guid").toString());

device_guid_no = deviceArray.getJSONObject(i).getString("guid")
.toString();

JSONArray serviceArray = deviceArray.getJSONObject(i).getJSONArray(
"services");

for (int j = 0; j < serviceArray.length(); j++) {



System.out.println("service_version="
+ serviceArray.getJSONObject(j).getString("version")
.toString());

System.out.println("service_private_ip="
+ serviceArray.getJSONObject(j).getString("private_ip")
.toString());

System.out.println("service_name="
+ serviceArray.getJSONObject(j).getString("name")
.toString());

}
}

}





Your project contains error(s), please fix it before running it


That usually comes form errors in the build path
if your using eclipse there is a view you can add that list all the errors called "Problems"
otherwise you can try to clean the project that usually solve a few problems
finally if you add or altered resources from outside your IDE you'll want to reload the resources and clean
EDIT (Comment by anonymous user)
This can also be caused by an out of date "Debug Certificate" fixed as follows:
Delete your debug certificate under ~/.android/debug.keystore (on Linux and Mac OS X); the directory is something like %USERPROFILE%/.androidon Windows.
The Eclipse plugin should then generate a new certificate when you next try to build a debug package. You may need to clean and then build to generate the certificate.
This is also another fix for the "setContentView(R.layout.main);" error that says it cannot find R.layout.main when it is actually generated. (R cannot be resolved to a variable).
This is also another fix for the error "Your project has errors..." and you cannot find any. Clean and rebuild are still necessary after generating a new debug certificate.

It's a pain to have to delete all your development .apk files, because the new certificate doesn't match so you can't upgrade them in all your AVDs. You have to get another development MAP-API key as well. There's another solution.
You can create your own debug certificate in debug.keystore with whatever expiration you want. Do this in the .android folder under your HOME directory:
keytool -genkey -v -keystore debug.keystore -alias androiddebugkey -storepass android -keypass android -keyalg RSA -validity 14000
ADT sets the first and last name on the certificate as "Android Debug", the organizational unit as "Android" and the two-letter country code as "US". You can leave the organization, city, and state values as "Unknown". This example uses a validity of 14000 days. You can use whatever value you like.