0

hi guys i want to sent location name and some other string values to the server...i am new in android so i dont know much about it....i pass the location and other values with url...url hits but the values are not receive by the server..help please me out...

public class SearchResult extends AppCompatActivity {

private ListView lvSearch;
private ProgressDialog dialog;
private final String URL_TO_HIT = "http://www.abcd.com/mobile_search.php";
private String location = "bathinda";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_result);
    setContentView(R.layout.activity_search_result);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("Search Result");
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            onBackPressed();
        }
    });
    dialog = new ProgressDialog(this);
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);
    dialog.setMessage("Loading. Please wait...");
    // Create default options which will be used for every
    //  displayImage(...) call if no options will be passed to this method
    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .build();
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions)
            .build();
    ImageLoader.getInstance().init(config); // Do it on Application start

    lvSearch = (ListView)findViewById(R.id.lvSearch);

    Bundle bundle = getIntent().getExtras();
    String bar = bundle.getString("bar");
    String nights = bundle.getString("nights");
    String nearby = bundle.getString("nearby");
    String deals = bundle.getString("deals");

    // To start fetching the data when app start, uncomment below line to start the async task.
    new JSONTask().execute(URL_TO_HIT, location, bar, nights, nearby, deals );

}
public class JSONTask extends AsyncTask<String,String, List<SearchData> >{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog.show();
    }

    @Override
    protected List<SearchData> doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            StringBuffer buffer = new StringBuffer();
            String line ="";
            while ((line = reader.readLine()) != null){
                buffer.append(line);
            }

            String finalJson = buffer.toString();

            JSONObject parentObject = new JSONObject(finalJson);
            JSONArray parentArray = parentObject.getJSONArray("search");

            List<SearchData> searchDataList = new ArrayList<>();

            Gson gson = new Gson();
            for(int i=0; i<parentArray.length(); i++) {
                JSONObject finalObject = parentArray.getJSONObject(i);
                /**
                 * below single line of code from Gson saves you from writing the json parsing yourself which is commented below
                 */
                SearchData searchData = gson.fromJson(finalObject.toString(), SearchData.class);

                searchDataList.add(searchData);
            }
            return searchDataList;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if(connection != null) {
                connection.disconnect();
            }
            try {
                if(reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return  null;
    }

    @Override
    protected void onPostExecute(final List<SearchData> result) {
        super.onPostExecute(result);
        dialog.dismiss();
        if(result != null) {
            SearchAdapter adapter = new SearchAdapter(getApplicationContext(), R.layout.searchresultrow, result);
            lvSearch.setAdapter(adapter);

        } else {
            Toast.makeText(getApplicationContext(), "Not able to fetch data from server, please check internet", Toast.LENGTH_SHORT).show();
        }
    }
}

public class SearchAdapter extends ArrayAdapter{

    private List<SearchData> searchDataList;
    private int resource;
    private LayoutInflater inflater;
    public SearchAdapter(Context context, int resource, List<SearchData> objects) {
        super(context, resource, objects);
        searchDataList = objects;
        this.resource = resource;
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        if(convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(resource, null);
            holder.searchimg11 = (ImageView)convertView.findViewById(R.id.searchimg1);
            holder.barname1 = (TextView)convertView.findViewById(R.id.barname);
            holder.address1 = (TextView)convertView.findViewById(R.id.address);
            holder.offer1 = (TextView)convertView.findViewById(R.id.offer);
            holder.hourtext1 = (TextView)convertView.findViewById(R.id.hourtext);
            holder.coststext1 = (TextView)convertView.findViewById(R.id.coststext);
            holder.textv11 = (TextView)convertView.findViewById(R.id.textv1);
            holder.featuredtext1 = (TextView)convertView.findViewById(R.id.featuredtext);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //final ProgressBar progressBar = (ProgressBar)convertView.findViewById(R.id.progressBar);

        // Then later, when you want to display image
        ImageLoader.getInstance().displayImage(searchDataList.get(position).getBar_image(), holder.searchimg11, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                // progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                //progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                // progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                // progressBar.setVisibility(View.GONE);
            }
        });

        holder.barname1.setText(searchDataList.get(position).getBarname());
        holder.address1.setText(searchDataList.get(position).getLocation());
        holder.offer1.setText( searchDataList.get(position).getOffers());
        holder.hourtext1.setText( searchDataList.get(position).getOpen_hours());
        holder.coststext1.setText(searchDataList.get(position).getCost_for_two());
        holder.textv11.setText(searchDataList.get(position).getFreebe());
        holder.featuredtext1.setText(searchDataList.get(position).getFeaured());
        return convertView;
    }

    class ViewHolder{
        private ImageView searchimg11;
        private TextView address1;
        private TextView offer1;
        private TextView hourtext1;
        private TextView coststext1;
        private TextView textv11;
        private TextView barname1;
        private TextView featuredtext1;
    }

}

}

  • where do you pass your parameters to the url? I don't see it in your code. You pass parameters into the `doInBackground`, but you don't seem to pass them to the server – x0r May 21 '16 at 06:32
  • The params in doInBackground contains reference to however many items you pass it. params[0] is the url, but you need all the other references passed in to the request as well. – paul_hundal May 21 '16 at 06:55
  • @paul_hundal can you please paste the code like that u said.. – Abhijeet Singh May 21 '16 at 07:53

2 Answers2

0

You are not passing any parameters to the server there . You are calling URL without any parameters

Read this solution to know how to pass parameters to HttpURLConnection using POST

How to add parameters to HttpURLConnection using POST

Community
  • 1
  • 1
Pradeep Charan
  • 653
  • 2
  • 7
  • 28
0

I did not go through your code. I suggest you to use the library to do much of the work instead of you developing on top of HTTP stack.

Use Retrofit(http://square.github.io/retrofit/) or Volley(https://developer.android.com/training/volley/index.html). Retrofit is easy to use and manage but volley give you lot of control. Since you are new to programming on the client side, I suggest you use the Retrofit. You can't go wrong with sending JSON data, and few post using this libraries.

ZAN
  • 86
  • 5