2015-10-05 53 views
0

我是新来的Java和Android开发,并尝试创建一个简单的应用程序,它应该联系Web服务器A并发送,使用http get将一些数据添加到文本。如何使用获取请求发送数据到服务器javascript

我有简单的HTML代码一些javascript(服务器A)

<html> 
<head> 
<title>This is my Webpage</title>`enter code here` 
<h1>My Example</h1> 
     <script> 
function myFunction(){ 
document.getElementById("myid").value=$ab; 
} 
</script 
    </head> 
    <body onload="myFunction()"> 
     <input id="myid" type="text" /> 
    </body> 
</html> 

和我的Android代码发送HTTP请求到本地(服务器A)

public class MainActivity extends Activity { 

private Button button; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    button=(Button) findViewById(R.id.click); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String url = "http://www.localhost/tuan/example.html"; 
      MyCommandTask task = new MyCommandTask(); 
      task.execute(url); 
     } 
    }); 
} 

public class MyCommandTask extends AsyncTask<String,Void,Document> 
{ 

    @Override 
    protected Document doInBackground(String... params) { 
     String url=params[0]; 
     try { 
      HttpGet httpGet = new HttpGet(url); 
     } 
     catch (Exception ex) 
     { 
      ex.printStackTrace(); 
     } 
     return null; 
    } 
    @Override 
    protected void onPostExecute(Document document) { 
     super.onPostExecute(document); 
    } 
} 
}`` 

现在我想送文本数据并在(服务器A)上显示文本结果。 请大家帮帮我。

回答

0

看看这家伙。 http://developer.android.com/training/basics/network-ops/connecting.html#download。既然你已经在doInBackground()方法了URL字符串,用下面的代码

InputStream is = null; 
// Only display the first 500 characters of the retrieved 
// web page content. 
int len = 500; 

try { 
    URL url = new URL(myurl); 
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
    conn.setReadTimeout(10000 /* milliseconds */); 
    conn.setConnectTimeout(15000 /* milliseconds */); 
    conn.setRequestMethod("GET"); 
    conn.setDoInput(true); 
    // Starts the query 
    conn.connect(); 
    int response = conn.getResponseCode(); 
    Log.d(DEBUG_TAG, "The response is: " + response); 
    is = conn.getInputStream(); 

    // Convert the InputStream into a string 
    String contentAsString = readIt(is, len); 
    return contentAsString; 

// Makes sure that the InputStream is closed after the app is 
// finished using it. 
} finally { 
    if (is != null) { 
     is.close(); 
    } 
} 

不要忘记更改doInBackground(返还型)字符串为好。如果你想更进一步,请尝试抓取抽球,这是一个了不起的网络库https://developer.android.com/training/volley/index.html

+0

感谢@ SANNY,但如何显示文本数据(服务器A):( –

+0

如果u想将数据发送到服务器,你需要使用POST作为requestMethod。我张贴新的答案检查出来,伙计:-) – Sanny

-1

这里是如何将数据发布到服务器。把这些线内doInBackground()

private static final String POST_PARAMS = "userName=Pankaj"; 
    URL obj = new URL(POST_URL); 
    HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 
    con.setRequestMethod("POST"); 
    con.setRequestProperty("User-Agent", USER_AGENT); 

    // For POST only - START 
    con.setDoOutput(true); 
    OutputStream os = con.getOutputStream(); 
    os.write(POST_PARAMS.getBytes()); 
    os.flush(); 
    os.close(); 
    // For POST only - END 

    int responseCode = con.getResponseCode(); 
    System.out.println("POST Response Code :: " + responseCode); 

    if (responseCode == HttpURLConnection.HTTP_OK) { //success 
     BufferedReader in = new BufferedReader(new InputStreamReader(
       con.getInputStream())); 
     String inputLine; 
     StringBuffer response = new StringBuffer(); 

     while ((inputLine = in.readLine()) != null) { 
      response.append(inputLine); 
     } 
     in.close(); 

     // print result 
     System.out.println(response.toString()); 
    } else { 
     System.out.println("POST request not worked"); 
    } 

这里是source

相关问题