|
|
JSON-
A dense indexed sequence of values. Values may be any mix of
JSONObjects, other JSONArrays, Strings, Booleans, Integers, Longs, Doubles, null or NULL. Values may not be NaNs, infinities, or of any type not listed here.JSONArray has the same type coercion behavior and optional/mandatory accessors as JSONObject. See that class' documentation for details.
Warning: this class represents null in two incompatible ways: the standard Java
null reference, and the sentinel value NULL. In particular, get fails if the requested index holds the null reference, but succeeds if it holds JSONObject.NULL.
Instances of this class are not thread safe. Although this class is nonfinal, it was not designed for inheritance and should not be subclassed. In particular, self-use by overridable methods is not specified. See Effective Java Item 17, "Design and Document or inheritance or else prohibit it" for further information.
modifiable set of name/value mappings. Names are unique, non-null strings. Values may be any mix of
JSONObjects, JSONArrays, Strings, Booleans, Integers, Longs, Doubles or NULL. Values may not be null, NaNs, infinities, or of any type not listed here.
This class can coerce values to another type when requested.
- When the requested type is a boolean, strings will be coerced using a case-insensitive comparison to "true" and "false".
- When the requested type is a double, other
Numbertypes will be coerced usingdoubleValue. Strings that can be coerced usingvalueOf(String)will be. - When the requested type is an int, other
Numbertypes will be coerced usingintValue. Strings that can be coerced usingvalueOf(String)will be, and then cast to int. - When the requested type is a long, other
Numbertypes will be coerced usinglongValue. Strings that can be coerced usingvalueOf(String)will be, and then cast to long. This two-step conversion is lossy for very large values. For example, the string "9223372036854775806" yields the long 9223372036854775807. - When the requested type is a String, other non-null values will be coerced using
valueOf(Object). Although null cannot be coerced, the sentinel valueNULLis coerced to the string "null".
This class can look up both mandatory and optional values:
- Use
getType()to retrieve a mandatory value. This fails with aJSONExceptionif the requested name has no value or if the value cannot be coerced to the requested type. - Use
optType()to retrieve an optional value. This returns a system- or user-supplied default if the requested name has no value or if the value cannot be coerced to the requested type.
Warning: this class represents null in two incompatible ways: the standard Java
null reference, and the sentinel value NULL. In particular, calling put(name, null) removes the named entry from the object but put(name, JSONObject.NULL) stores an entry whose value is JSONObject.NULL.
Instances of this class are not thread safe. Although this class is nonfinal, it was not designed for inheritance and should not be subclassed. In particular, self-use by overrideable methods is not specified. See Effective Java Item 17, "Design and Document or inheritance or else prohibit it" for further information.
1 step-
Create a Android Application Project "JSON_Project".
2 step-
Create a XML file "httpex.xml".
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tvHttp"
/>
</LinearLayout>
3 step-
Create a Java File " HttpExample.java ".
package com.example.counterproject;
import java.io.IOException;
import java.security.PublicKey;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.R.string;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class HttpExample extends Activity {
final static String URL="http://twitter.com";
TextView httpStuff;
HttpClient client;
JSONObject json;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.httpex);
httpStuff=(TextView)findViewById(R.id.tvHttp);
client=new DefaultHttpClient();
new Read().execute("text");
}
public JSONObject lastTweet(String username) throws ClientProtocolException, IOException, JSONException{
StringBuilder url=new StringBuilder(URL);
url.append(username);
HttpGet get=new HttpGet(url.toString());
HttpResponse r=client.execute(get);
int status=r.getStatusLine().getStatusCode();
if(status == 200)
{
HttpEntity e = r.getEntity();
String data=EntityUtils.toString(e);
JSONArray timeline= new JSONArray(data);
JSONObject last=timeline.getJSONObject(0);
return last;
}
else
{
Toast.makeText(HttpExample.this, "error", Toast.LENGTH_SHORT);
return null;
}
}
public class Read extends AsyncTask <String, Integer, String>
{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try{
json=lastTweet("mybringback");
return json.getString(params[0]);
}catch (ClientProtocolException e) {
// TODO: handle exception
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}catch (JSONException e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
httpStuff.setText(result);
}
}
}
4 step-
Add permission in Android manifest file (Internet permission).
0 comments:
Post a Comment