Tuesday, November 27, 2012

Android Post a JSON Object to the WEB SERVICE.


Android Post a JSON Object to the WEB SERVICE.

Impotent:
here developer better use the posting with in separate thread, if you want to use in main thread it's getting exception.

(any button click)button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { 

final int TIMEOUT_MILLISEC = 10000;  // = 10 seconds
Thread t = new Thread(){
       public void run() {
        Looper.prepare(); 
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
    HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
    HttpClient client = new DefaultHttpClient(httpParams);
    HttpPost request = new HttpPost("----- any post url here -----");
    //set post request type
    request.setHeader(HTTP.CONTENT_TYPE, "application/json; charset=utf-8");
    //request result type
    request.setHeader("Accept", "application/json; charset=utf-8");
JSONObject json = new JSONObject();
    try {
json.put("object_key1", "Value1").toString();// Example key, values 
json.put("object_key2", "Value2").toString();
json.put("object_key3", "Value3").toString();
JSONArray mainArr = new JSONArray();
   for(int i = 0; i < 3; i++){      
       JSONObject mainObj = new JSONObject();
       
   mainObj.put("key1", "any Value");
   mainObj.put("key2", "any Value");
       
   JSONArray innerArr = new JSONArray();
       
   for(int k = 0; k < 4 ; k++){        
       JSONObject innerObj = new JSONObject();
       innerObj.put("inner_key1", "any Value");
       
       innerArr.put(innerObj);
    }

  mainObj.put("innerArr", innerArr).toString();        
  mainArr(mainObj);
       }   
       
       json.put("inner_array", mainArr).toString();
Log.i("jason Object", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
request.setEntity(se);      
HttpResponse response = client.execute(request); 
String temp = EntityUtils.toString(response.getEntity());
       Log.i("tag_responce", temp);
} catch (Exception e) {
e.printStackTrace();
}
Looper.loop(); //Loop in the message queue
           }
       };
       t.start();  
       
       }
   });

Thursday, November 15, 2012

ScrollView inside ScrollView Scrolling problem


While designing rich layouts you might need to use two scrollview in your app.
Well ideally its not advised to use two scrollview in a view. So try to avoid it.

Why this problem occurs ? :

When you put two scrollview android just get confused which scroll view is touched. So sometimes it gets unable to deliver touch event.
But even if the requirement forces you to make such layouts. Try this…
Say case is somewhat like this….
<ScrollView android:id=”@+id/parent_scroll” 
            android:layout_width=”fill_parent”
            android:layout_height=”wrap_content”
            android:layout_weight=”1″
            android:background=”@drawable/dotted_bg”
            android:focusableInTouchMode=”false”>
                        <LinearLayout   />
                        <LinearLayout   />
                        <LinearLayout  >
                        <ScrollView android:id=”@+id/child_scroll”  
                        android:layout_width=”fill_parent”
                        android:layout_height=”fill_parent”
                        android:background=”@drawable/text_box_bg”>
                    <TextView android:id=”@+id/text_description”
                        android:layout_width=”fill_parent”
                        android:layout_height=”fill_parent”
                        android:textColor=”@color/gray”
                        android:textSize=”12dip”
                        android:padding=”5dip”
                        android:scrollbars=”vertical”/>
                    <!–ScrollView>
                  </LinearLayout>
</ScrollView>

Step 1 : Provide unique id to both the scrollview.
Step 2 : get reference of that two scrollview in your activity.

     parentScroll=(ScrollView)findViewById(R.id.parent_scroll);
     childScroll=(ScrollView)findViewById(R.id.child_scroll);

Step 3: Now set touch listeners for both.

            parentScroll.setOnTouchListener(new View.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {
                    Log.v(TAG,”PARENT TOUCH”);
                    findViewById(R.id.child_scroll).getParent().requestDisallowInterceptTouchEvent(false);
                    return false;
                }
            });
            childScroll.setOnTouchListener(new View.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) 
                {
                    Log.v(TAG,”CHILD TOUCH”);
                                        // Disallow the touch request for parent scroll on touch of child view
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    return false;
                }
            });
Done …