如何安卓插件编辑文本添加到URL?机器人活动 [英] How to add android add edit text to a url? android activity

查看:229
本文介绍了如何安卓插件编辑文本添加到URL?机器人活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个用户输入一个网址。 我解析一个JSON,所以我想在编辑文本添加到URL格式的URL 使得在用户查询中的数据将被解析 例如 www.google.com 是previous 1 所以我想将用户输入添加到它像 www.google.com/q = 用户输入 任何帮助... 我解析的数据在Android中使用..的这个教程

I want to add a User input to a url. I am parsing a JSON form a URL so I want to add a Edit text to that url so that on user query the data will be parsed example www.google.com is previous one so I want to add a user input to it like www.google.com/q= user input any help... I am parsing the data in android.. with this tutorial

我使用的网址为的JSONObject = JSONfunctions                .getJSONfromURL(http://10.0.2.2:8080/jsondatad/);

I using url at jsonobject = JSONfunctions .getJSONfromURL("http://10.0.2.2:8080/jsondatad/");

如何在这里添加数据 请帮我的任何例子,我是新来的Andr​​oid和Java的

how to add the data over here please help me with any example I am new to android and java

推荐答案

您编辑文本

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

获取从编辑文本的用户数据作为休耕。

Get user data from edit text as fallows ..

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

现在,当你需要把这些数据的URL。使用此类似。

Now when you need to put that data on url .. use this like ..

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

更新::

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

更新:::

UPDATE :::

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" .. 


        }

    }

}

这篇关于如何安卓插件编辑文本添加到URL?机器人活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆