Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用JSON将数据从mysql显示到android studio_Php_Android_Json_Android Studio_Xampp - Fatal编程技术网

Php 使用JSON将数据从mysql显示到android studio

Php 使用JSON将数据从mysql显示到android studio,php,android,json,android-studio,xampp,Php,Android,Json,Android Studio,Xampp,我想使用textview将mysql中的数据显示到android中,但当我运行应用程序时,它会自动停止, 有人能帮我修一下吗,, 有什么好主意吗 dbconfig.php send-data.php MainActivity.java 包flix.yudi.kuesioner2; 导入java.io.IOException; 导入org.apache.http.HttpResponse; 导入org.apache.http.client.ClientProtocolException;

我想使用textview将mysql中的数据显示到android中,但当我运行应用程序时,它会自动停止, 有人能帮我修一下吗,, 有什么好主意吗

dbconfig.php


send-data.php


MainActivity.java

包flix.yudi.kuesioner2;
导入java.io.IOException;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.util.EntityUtils;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.net.Uri;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.ProgressBar;
导入android.widget.TextView;
公共类MainActivity扩展了活动{
文本视图文本视图;
JSONObject json=null;
字符串str=“”;
HttpResponse响应;
语境;
ProgressBar ProgressBar;
按钮;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressbar=(progressbar)findViewById(R.id.progressBar1);
textview=(textview)findViewById(R.id.textView1);
按钮=(按钮)findViewById(R.id.button1);
progressbar.setVisibility(View.GONE);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
progressbar.setVisibility(View.VISIBLE);
新建GetTextViewData(context.execute();
}
});
}
私有类GetTextViewData扩展异步任务
{
公共语境;
公共GetTextViewData(上下文)
{
this.context=上下文;
}
@凌驾
受保护的void onPreExecute()
{
super.onPreExecute();
}
@凌驾
受保护的Void doInBackground(Void…arg0)
{
HttpClient myClient=新的默认HttpClient();
HttpPost myConnection=newhttppost(“192.168.1.139/a_test/send data.php”);
试一试{
response=myClient.execute(myConnection);
str=EntityUtils.toString(response.getEntity(),“UTF-8”);
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
JSONArray jArray=新JSONArray(str);
json=jArray.getJSONObject(0);
}捕获(JSONException e){
e、 printStackTrace();
}
捕获(例外e)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(void结果)
{
试一试{
setText(json.getString(“ServerData”));
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//加载完TextView后隐藏进度条。
progressbar.setVisibility(View.GONE);
}
}
}
activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.jsonparsingfromurltextview_android_examples.com.MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="TextView Static Text Before Load"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center"/>

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="17dp" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="150dp"
    android:text="Click Here to Load TextView data dynamically from MySQL Database Online Using JSON Parsing" />
我试着想办法解决,但还是一无所获, 请帮帮我


尝试向
URL
添加一些方案,如
http
https

http://192.168.1.139/a_test/send-data.php

尝试向
URL
添加一些方案,如
http
https

http://192.168.1.139/a_test/send-data.php

首先,试试你的邮递员服务电话。。。检查您是否从192.168.1.139/a_test/send-data.php获得了正确的响应

跟随获得邮递员插件

检查下面的代码

 private String makeRESTService(String methodNameWithUrl) throws JSONException {
        final String KEY_USER_NAME = "username";
        final String KEY_PASSWORD = "password";
        System.out.println("url" + methodNameWithUrl);
        String jsonResult = null;
        String jsonObj = null;

        //create a json string
        JSONObject parent = new JSONObject();
        parent.put(KEY_USER_NAME, "manu");
        parent.put(KEY_PASSWORD, "1234");
        Log.e("To json", jsonObj);

        URL url = null;

        HttpURLConnection urlConnection = null;
        try {
            url = new URL("your url"); //place your url
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setReadTimeout(10000);
            urlConnection.setConnectTimeout(15000);

            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Content-Type", "application/json");
            OutputStream os = urlConnection.getOutputStream();

            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(jsonObj);
            writer.flush();
            writer.close();
            os.close();

            urlConnection.connect();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            jsonResult = inputStreamToString(in)
                    .toString();
            Log.e("jsonResult", jsonResult);

            //   readStream(in);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            urlConnection.disconnect();
        }
        return jsonResult;

    }


    private StringBuilder inputStreamToString(InputStream is) {
        String rLine = "";
        StringBuilder answer = new StringBuilder();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader rd = new BufferedReader(isr);
        try {
            while ((rLine = rd.readLine()) != null) {
                answer.append(rLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return answer;
    }

首先,试试你的邮递员服务电话。。。检查您是否从192.168.1.139/a_test/send-data.php获得了正确的响应

跟随获得邮递员插件

检查下面的代码

 private String makeRESTService(String methodNameWithUrl) throws JSONException {
        final String KEY_USER_NAME = "username";
        final String KEY_PASSWORD = "password";
        System.out.println("url" + methodNameWithUrl);
        String jsonResult = null;
        String jsonObj = null;

        //create a json string
        JSONObject parent = new JSONObject();
        parent.put(KEY_USER_NAME, "manu");
        parent.put(KEY_PASSWORD, "1234");
        Log.e("To json", jsonObj);

        URL url = null;

        HttpURLConnection urlConnection = null;
        try {
            url = new URL("your url"); //place your url
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setReadTimeout(10000);
            urlConnection.setConnectTimeout(15000);

            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Content-Type", "application/json");
            OutputStream os = urlConnection.getOutputStream();

            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(jsonObj);
            writer.flush();
            writer.close();
            os.close();

            urlConnection.connect();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            jsonResult = inputStreamToString(in)
                    .toString();
            Log.e("jsonResult", jsonResult);

            //   readStream(in);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            urlConnection.disconnect();
        }
        return jsonResult;

    }


    private StringBuilder inputStreamToString(InputStream is) {
        String rLine = "";
        StringBuilder answer = new StringBuilder();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader rd = new BufferedReader(isr);
        try {
            while ((rLine = rd.readLine()) != null) {
                answer.append(rLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return answer;
    }

尝试下载Postman,然后尝试向您的url发送post请求。你有回复吗?谢谢你的建议,我会尝试找到如何使用它,因为我不知道,,,应该很简单,它很好而且直观。我已经编辑了我的帖子,关于另一个应用程序(如postman)的回复截图,现在我下一步该怎么做,先生?你有什么想法吗?试着下载Postman,然后试着向你的url发送一个post请求。你有回复吗?谢谢你的建议,我会尝试找到如何使用它,因为我不知道,,,应该很简单,它很好而且直观。我已经编辑了我的帖子,关于另一个应用程序(如postman)的回复截图,现在我下一步该怎么做,先生?你有什么想法吗?谢谢你的代码和参考资料,我试试看,你还有其他firef*x应用程序吗?但是我应该把你的代码放在哪里呢?我应该创建新的java类吗?在AsyncTask的doinBackground中,我已经附加了一个来自服务器的屏幕截图,先生,你怎么看?我做错什么了吗?谢谢你的代码和你的参考,我试试,你有其他firef*x应用程序吗?但是我应该把你的代码放在哪里呢?我应该创建新的java类吗?在AsyncTask的doinBackground中,我已经附加了一个来自服务器的屏幕截图,先生,你怎么看?我做错什么了吗?我在URL中添加了http或https,但它仍然强制关闭自己,先生,你有其他想法吗?我在URL中添加了http或https,但它仍然强制关闭自己,先生,你有其他想法吗?