0

I would like give comment to my Image ,when i give one word as a comment then it successfully saved using asyntask, but when i have send comment like "asas sas asas asas asas asas asas asa " means line with spaces it give null response. Please give me suggestions.

My asyntask code is,

package com.asyntask;

import java.util.ArrayList;
import java.util.concurrent.TimeoutException;

import org.json.JSONException;
import org.json.JSONObject;

import com.model_classes.Model_Image_list;
import com.model_classes.Model_User_info;
import com.model_classes.Model_Video_list;
import com.utility.BaseUrl;
import com.utility.ConnectionDetector;
import com.utility.CustomDialogue;
import com.utility.JSONfunctions;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.os.AsyncTask;

//DownloadJSON AsyncTask
public  class CommentListAsynTask extends AsyncTask<String, String, JSONObject>
{ 
    int flag=0;
    String userID,video_title,type;
    Model_Image_list image_list_info;
    Model_Video_list video_list_info;
    ProgressDialog mProgressDialog;
    Context context;
    ArrayList<Model_Video_list> arraylist_video_list=new ArrayList<Model_Video_list>();
    ArrayList<Model_Image_list> arraylist_image_list;
    ArrayList<Model_User_info> arraylist_user_info;
    JSONObject jsonobjectResult;
    String str_status,action,viewId,comment;
    public CommentListAsynTask(Context context) 
    {
        this.context=context;
    }

    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();


                mProgressDialog = new ProgressDialog(context);
                mProgressDialog.setMessage("Please wait...");
                mProgressDialog.setCancelable(false);
        arraylist_image_list = new ArrayList<Model_Image_list>();

        mProgressDialog.show();

    }


    protected JSONObject doInBackground(String... arg)

    {
        this.userID=arg[0];
        System.out.println(userID+".......UserID");
        this.viewId=arg[1];
        System.out.println(viewId+".......viewID");
        this.comment=arg[2];
        System.out.println(comment+".......Comments");
        this.action=arg[3];
        System.out.println(action+".......actionID");

        // Create an array
        arraylist_user_info = new ArrayList<Model_User_info>();


        //Send URL to JSONfunction class in which parsing the JSON data.
        try 
        {
            jsonobjectResult = JSONfunctions.getJSONfromURL(BaseUrl.URL+"commentImageVideo.php?userIdForComment="
                    +userID+"&viewIdForComment="+viewId+"&comment="+comment+"&actionComment="+action);
            System.out.println(jsonobjectResult+"................Result");

            // Locate the array name in JSON
            if(jsonobjectResult !=null)
            {   if(jsonobjectResult.has("error"))
            {
                flag=1;
            }
            if(jsonobjectResult.has("null"))
            {
                flag=1;
            }
            if(jsonobjectResult.has(""))
            {
                flag=1;

            }
            else
            {       
                str_status=jsonobjectResult.getString("status");
                System.out.println(str_status+"..........>Ststus");
            }
            }


        }
        catch (NotFoundException e)
        {
            e.printStackTrace();
        } catch (TimeoutException e) 
        {
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return jsonobjectResult;


    }

    @Override
    protected void onPostExecute(JSONObject response) 
    {  
            mProgressDialog.dismiss();
        if(flag != 1)
        {
            try {
                if(response.getString("status").equalsIgnoreCase("Success."))
                {
                    System.out.println(str_status+".............Comment status");
                    CustomDialogue dialogue=new CustomDialogue(context, "You have successfully Comment");
                    dialogue.show();
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            /*if(str_status.equalsIgnoreCase("Error."))
            {
                CustomDialogue dialogue=new CustomDialogue(context, "You have Already liked");
                dialogue.show();

            }*/
        }
        else
        {
            ConnectionDetector.displaySlowNetworkDialog((Activity)context);
        }

    }
}

and my json parsing function is,

public static JSONObject getJSONfromURL(String url) throws TimeoutException
    {
        InputStream is = null;
        String result = "";
        JSONObject jsonObject = null;

        // Download JSON data from URL
        try {
            HttpParams httpParams = new BasicHttpParams();
            // Set the timeout in milliseconds until a connection is established.
            int timeoutConnection = 150000;
            ConnManagerParams.setTimeout(httpParams, timeoutConnection);
            HttpConnectionParams.setConnectionTimeout(httpParams,timeoutConnection);

            // in milliseconds which is the timeout for waiting for data.
            HttpConnectionParams.setSoTimeout(httpParams, timeoutConnection);

            DefaultHttpClient httpclient = new DefaultHttpClient(httpParams);
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder stringBuilder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line + "\n");
            }
            is.close();
            result = stringBuilder.toString();
                jsonObject=new JSONObject(result);


        } 
        catch (ConnectTimeoutException e)
        {   
            try
            {
                //Create jsonobject for handling ConnectTimeoutException.
                jsonObject = new JSONObject("{\"error\":\"ConnectTimeoutException\"}");
            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        catch (SocketTimeoutException  e) 
        {
            try 
            {
                //Create jsonobject for handling SocketTimeoutException.
                jsonObject = new JSONObject("{\"error\":\"SocketTimeoutException\"}");
            } catch (JSONException e1) 
            {
                e1.printStackTrace();
            }
            Log.e("log_tag", "Error in http connection for slow connection ... " + e.toString());
        }
        catch (Exception e) 
        {
            try {
                //Create jsonobject for handling for null Exception.
                jsonObject=new JSONObject("{\"null\":\"JSON null\"}");
            } catch (JSONException e1) 
            {
                e1.printStackTrace();
            }

        }



        return jsonObject;
    }
1
  • In catch (Exception e), print the exception. That will help understand the issue. Commented Jun 11, 2014 at 2:14

1 Answer 1

1

Though not sure this will solve your problem, try setting %20 for your comment before sending your getRequest.

comment = comment.replaceAll(" ","%20"); //escape spaces trim() can also be useful
jsonobjectResult = JSONfunctions.getJSONfromURL(BaseUrl.URL+"commentImageVideo.php?userIdForComment="
                +userID+"&viewIdForComment="+viewId+"&comment="+comment+"&actionComment="+action);
Sign up to request clarification or add additional context in comments.

2 Comments

This is likely to be the problem. But better yet, use Uri.Builder instead.
Thanks... Using replace All() , it solved,but Matish how Uri.Builder is used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.