JSONException PHP Android

时间:2015-11-07 19:09:42

标签: php android

你能帮我解决这个问题吗? 所以我一直在寻找从远程数据库获取数据。 我已经下载并安装了WAMP服务器并连接到远程数据库,我添加了一些行ecc ... 使用PhP风暴,我创建了似乎有用的脚本,它显示了我需要的所有内容。 问题是logcat,你能告诉我该如何解决它吗?

    11-07 20:12:21.139 17619-17619/com.example.arnold.itsosgadda E/log_data: Comunicazione n.129  <a href="http://www.itsosgadda.it/comunicazioni/pubbliche/2015-16%20Comunicazione%20129%20Uscita%20didattica%203B%20museo%20del%20computer%20Fornovo.pdf">Download<br>
11-07 20:12:21.139 17619-17619/com.example.arnold.itsosgadda W/System.err: org.json.JSONException: Value Comunicazione of type java.lang.String cannot be converted to JSONArray
11-07 20:12:21.149 17619-17619/com.example.arnold.itsosgadda W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
11-07 20:12:21.149 17619-17619/com.example.arnold.itsosgadda W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:96)
11-07 20:12:21.149 17619-17619/com.example.arnold.itsosgadda W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:108)
11-07 20:12:21.149 17619-17619/com.example.arnold.itsosgadda W/System.err:     at com.example.arnold.itsosgadda.CommunicationsActivity.connect(CommunicationsActivity.java:59)
11-07 20:12:21.149 17619-17619/com.example.arnold.itsosgadda W/System.err:     at com.example.arnold.itsosgadda.CommunicationsActivity.onCreate(CommunicationsActivity.java:36)
11-07 20:12:21.149 17619-17619/com.example.arnold.itsosgadda W/System.err:     at android.app.Activity.performCreate(Activity.java:5231)
11-07 20:12:21.149 17619-17619/com.example.arnold.itsosgadda W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-07 20:12:21.149 17619-17619/com.example.arnold.itsosgadda W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
11-07 20:12:21.149 17619-17619/com.example.arnold.itsosgadda W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2285)
11-07 20:12:21.149 17619-17619/com.example.arnold.itsosgadda W/System.err:     at android.app.ActivityThread.access$800(ActivityThread.java:138)
11-07 20:12:21.149 17619-17619/com.example.arnold.itsosgadda W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1236)
11-07 20:12:21.149 17619-17619/com.example.arnold.itsosgadda W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-07 20:12:21.149 17619-17619/com.example.arnold.itsosgadda W/System.err:     at android.os.Looper.loop(Looper.java:149)
11-07 20:12:21.149 17619-17619/com.example.arnold.itsosgadda W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5061)
11-07 20:12:21.149 17619-17619/com.example.arnold.itsosgadda W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
11-07 20:12:21.149 17619-17619/com.example.arnold.itsosgadda W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
11-07 20:12:21.159 17619-17619/com.example.arnold.itsosgadda W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
11-07 20:12:21.159 17619-17619/com.example.arnold.itsosgadda W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:603)
11-07 20:12:21.159 17619-17619/com.example.arnold.itsosgadda W/System.err:     at dalvik.system.NativeStart.main(Native Method)

这是我的PHP脚本

<?php
/**
 * Created by PhpStorm.
 * User: Arnol'd
 * Date: 07.11.2015
 * Time: 15:27
 */
$username = 'app';
$password = '4826159g';
$hostname = 'xxx.xxx.xxx.xxx:xxxx';
$database = 'app_db';
$conn = new mysqli($hostname, $username, $password, $database);
if ($conn->connect_error) {
    die("Connection failed:" . $conn->connect_error);
}
$sql = "select * from push";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo $row["push_name"] . '  <a href="'.$row['link'].'">' . 'Download' . "<br>";

    }
}
else{
    echo "0 results";
}
$conn->close();
?>

这里是我的java android代码

package com.example.arnold.itsosgadda;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("deprecation")
public class CommunicationsActivity extends Activity {
    private String jsonResult;
    private String url = "http://xxx.xxx.xxx.xxx:xxxx/config.php";
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.communications_news_layout);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        connect();

        ActionBar actionBar = getActionBar();
        assert actionBar != null;
        actionBar.setIcon(R.mipmap.ic_launcher);
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffeb3b")));
    }

    private void connect() {
        String data;
        List<String> listData = new ArrayList<>();
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(),
                android.R.layout.simple_list_item_1, listData);
        listView = (ListView) findViewById(R.id.listView);
        try {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost(url);
            request.setHeader("Accept","application/json");
            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
            data = EntityUtils.toString(entity);
            Log.e("log_data", data);
            try {
                JSONArray jsonArray = new JSONArray(data);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject obj = jsonArray.getJSONObject(i);
                    String name = obj.optString("push_name");
                    String link = obj.getString("link");
                    Log.e("log_data", name);

                    listData.add(name);
                    listData.add(link);
                    listView.setAdapter(adapter);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /*final CommunicationsActivity activity = this;
    final String url = "http://www.itsosgadda.it/index.php/comunicazioni-pubbliche";
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        ActionBar actionBar = getActionBar();
        assert actionBar != null;
        actionBar.setIcon(R.mipmap.ic_launcher);
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffeb3b")));
        //Shows me status bar progress

        setContentView(R.layout.communications_news_layout);
        WebView webView = (WebView) findViewById(R.id.webView);
        //Enables JS
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        //noinspection deprecation
        webView.getSettings().setDefaultZoom(FAR);
        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                activity.setTitle(R.string.loading_progress);
                activity.setProgress(progress * 100);
                if (progress == 100) {
                    activity.setTitle(R.string.app_name);
                }
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                // Handle the error
                Log.i("WEB_VIEW_COMMUNICATIONS", "Error code: " + errorCode);
                super.onReceivedError(view, errorCode, description, failingUrl);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webView.loadUrl(url);
    }*/

}

2 个答案:

答案 0 :(得分:0)

首先,您不应发布您的IP。在谈论您的问题时,您收到的HTML页面无法转换为JSON。它可能是错误或其他任何东西,尝试在尝试将其转换为JSON之前打印收到的响应,并且可能您会知道发生了什么

答案 1 :(得分:0)

你的json文件中有一个由php生成的错误。特别是android认为你提供的pdf链接不是JSONArray,也许它是一个json对象?建议你测试你的json http://jsonlint.com/并在这里发布你的整个json respone。