如何添加android添加编辑文本到网址? android活动

时间:2013-12-18 10:16:01

标签: android json url

我想在网址中添加用户输入。 我正在解析一个JSON形式的URL,所以我想在该URL中添加一个Edit文本 这样在用户查询时,数据将被解析 示例www.google.com是前一个 所以我想像www.google.com/q=用户输入一样添加用户输入 任何帮助...... 我正在使用this教程

解析android中的数据

我在jsonobject = JSONfunctions .getJSONfromURL("http://10.0.2.2:8080/jsondatad/");

使用网址

如何在这里添加数据 请帮我解决任何我刚接触android和java的例子

2 个答案:

答案 0 :(得分:1)

首先在android Activity

中优先使用EditText

喜欢这个::

EditText yourEditTextName=(EditText)findViewById(R.id.nameofedittext);

然后从EditText

获取文本
String your_url =  "www.google.com/q=" + yourEditTextName.getText().toString();

由于

答案 1 :(得分:1)

您的编辑文字

EditText your_edit_text = (EditText) findViewById(R.id.your_id);

将编辑文本中的用户数据视为休闲..

String edit_text_data = your_edit_text.getText().toString();

现在当你需要将这些数据放在url上时......使用它就像..

String your_url = "http://www.google.com=" + edit_text_data;

更新::

jsonobject = JSONfunctions .getJSONfromURL("http://10.0.2.2:8080/jsondatad/" + edit_text_data);

更新:::

package com.example.testapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

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.json.JSONArray;
import org.json.JSONObject;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AndroidHacker extends Activity{

    String my_edit_text_value;
    private ProgressDialog pDialog;

    HttpClient httpclient;
    HttpGet httpget;
    HttpResponse httpresponse;
    HttpResponse hhttpresponse;
    JSONObject myJsonObject = null;
    JSONArray myJsonArray = null;
    String myJsonString = "";

    JSONObject nmyJsonObject = null;
    JSONArray nmyJsonArray = null;
    String nmyJsonString = "";

    InputStream is;
    InputStreamReader isr;
    BufferedReader br;
    StringBuilder sb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        // Your edit text reference .. define edit text with id "et" in your layout.
        EditText et = (EditText) findViewById(R.id.et);

        // Your button reference ..define button with id "btn" in your layout.
        Button btn = (Button) findViewById(R.id.btn);

        // now when U click of button you can have edit text value with it ..
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                my_edit_text_value = et.getText().toString();

                           // Always use AsyncTask class to fetch data ..
                new BussinessOwnerHttpAsyncTask().execute();
            }
        });

    }


    class BussinessOwnerHttpAsyncTask extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            pDialog = new ProgressDialog(getParent());
            pDialog.setMessage("Please wait ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {


            HttpClient httpclient = new DefaultHttpClient();


            String myUrl = "http://10.0.2.2:8080/jsondatad/" + my_edit_text_value;




            String encodedURL = "";
            try {
                encodedURL = URLEncoder.encode(myUrl, "UTF-8");
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                URL url = new URL(encodedURL);
                Log.d("asca", ""+url);
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


            HttpGet httpget = new HttpGet(encodedURL);

            try {
                httpresponse = httpclient.execute(httpget);
                System.out.println("httpresponse" + httpresponse);
                Log.i("response", "Response" + httpresponse);
                InputStream is = httpresponse.getEntity().getContent();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                StringBuilder sb = new StringBuilder();

                String recievingDataFromServer = null;
                while ((recievingDataFromServer = br.readLine()) != null) {
                    Log.i("CHECK WHILE", "CHECK WHILE");
                    sb.append(recievingDataFromServer);
                }

                myJsonString = sb.toString();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return sb.toString();
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            pDialog.dismiss();
            // Do what ever U wish to do over here .. 

            // U have all value stored in "myJsonString" .. 


        }

    }

}