java-我可以在Android中使用POST方法从URL获取数据吗?

java-我可以在Android中使用POST方法从URL获取数据吗?,第1张

概述我想使用POST方法从URL提取数据.我已经使用GET方法获取了数据.现在,我想使用POST方法.当我单击按钮时,即使我提供了Internet许可,也没有任何响应.我认为还有其他问题.我曾尝试搜索许多站点并观看教程,但是我所获得的只是如何使用POST方法将数据发送到服务器.MainActivity.javapackage com.example.apipos

我想使用POST方法从URL提取数据.我已经使用GET方法获取了数据.现在,我想使用POST方法.当我单击按钮时,即使我提供了Internet许可,也没有任何响应.我认为还有其他问题.

我曾尝试搜索许多站点并观看教程,但是我所获得的只是如何使用POST方法将数据发送到服务器.

MainActivity.java

package com.example.APIpostmethod;import androID.os.StrictMode;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.TextVIEw;import androID.Widget.Toast;public class MainActivity extends AppCompatActivity {    public String data="";    public TextVIEw response;    public button btn;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        if (androID.os.Build.VERSION.SDK_INT > 15) {            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();            StrictMode.setThreadPolicy(policy);        }        response = findVIEwByID(R.ID.textVIEw);        btn = findVIEwByID(R.ID.button);        btn.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                new Async().execute();            }        });        data = Async.data();        Toast.makeText(getApplicationContext(),data,Toast.LENGTH_LONG).show();        response.setText(data);    }}

异步.java

package com.example.APIpostmethod;import androID.os.AsyncTask;import java.io.IOException;import java.net.httpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.Scanner;public class Async extends AsyncTask {    public String URline = "https://API.myJson.com/bins/uizi7";    public static String result="";    @OverrIDe    protected Object doInBackground(Object[] objects) {        try {            URL url = new URL(URline);            httpURLConnection httpURLConnection = (httpURLConnection)url.openConnection();            httpURLConnection.setRequestMethod("POST");            httpURLConnection.connect();            Scanner sc = new Scanner(url.openStream());            while(sc.hasNext())            {                result+=sc.nextline();            }        }        catch (MalformedURLException e) {            e.printstacktrace();        } catch (IOException e) {            e.printstacktrace();        }        return null;    }    public static String data(){        return result;        }}

activity_main.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:orIEntation="vertical"    tools:context="com.example.APIpostmethod.MainActivity">    <button        androID:ID="@+ID/button"        androID:layout_wIDth="236dp"        androID:layout_height="57dp"        androID:layout_marginStart="8dp"        androID:layout_marginleft="8dp"        androID:layout_margintop="8dp"        androID:layout_marginEnd="8dp"        androID:layout_marginRight="8dp"        androID:layout_marginBottom="8dp"        androID:text="button"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintHorizontal_bias="0.496"        app:layout_constraintStart_toStartOf="parent"        app:layout_constrainttop_totopOf="parent"        app:layout_constraintVertical_bias="0.898" />    <TextVIEw        androID:ID="@+ID/textVIEw"        androID:layout_wIDth="347dp"        androID:layout_height="531dp"        androID:layout_marginStart="8dp"        androID:layout_marginleft="8dp"        androID:layout_marginEnd="8dp"        androID:layout_marginRight="8dp"        androID:layout_marginBottom="8dp"        androID:text="Response::"        app:layout_constraintBottom_totopOf="@+ID/button"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constrainttop_totopOf="parent" /></linearLayout>

我连续不断地在logcat中收到此错误,例如无限次.

2019-06-06 16:03:48.315 1930-2875/? I/GnssLocationProvIDer: WakeLock acquired by sendMessage(REPORT_SV_STATUS,com.androID.server.location.GnssLocationProvIDer$SvstatusInfo@494d84f)2019-06-06 16:03:48.316 1930-1944/? I/GnssLocationProvIDer: WakeLock released by handleMessage(REPORT_SV_STATUS,com.androID.server.location.GnssLocationProvIDer$SvstatusInfo@494d84f)

我希望在textvIEw中获取数据,但是我什至看不到用textvIEw编写的文本.最佳答案将此用于使用httpURLConnection的POST请求,您可以根据需要添加或删除属性:

URL url = new URL("http://yoururl.com");httpsURLConnection conn = (httpsURLConnection) url.openConnection();conn.setReadTimeout(7000);conn.setConnectTimeout(10000);conn.setRequestMethod("POST");conn.setDoinput(true);conn.setDoOutput(true);List<nameValuePair> params = new ArrayList<nameValuePair>();params.add(new BasicnameValuePair("param1",paramValue1));params.add(new BasicnameValuePair("param2",paramValue2));OutputStream outputStream = conn.getoutputStream();BuffereDWriter writer = new BuffereDWriter(        new OutputStreamWriter(outputStream,"UTF-8"));writer.write(getquery(params));writer.flush();writer.close();outputStream.close();conn.connect();

这是getquery()方法:

private String getquery(List<nameValuePair> params) throws UnsupportedEnCodingException{    StringBuilder result = new StringBuilder();    boolean first = true;    for (nameValuePair pair : params)    {        if (first)            first = false;        else            result.append("&");        result.append(URLEncoder.encode(pair.getname(),"UTF-8"));        result.append("=");        result.append(URLEncoder.encode(pair.getValue(),"UTF-8"));    }    return result.toString();}
总结

以上是内存溢出为你收集整理的java-我可以在Android中使用POST方法从URL获取数据吗? 全部内容,希望文章能够帮你解决java-我可以在Android中使用POST方法从URL获取数据吗? 所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1145091.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存