从URL的Andr​​oid Retreiving JSON对象 [英] Android Retreiving JSON Object from URL

查看:121
本文介绍了从URL的Andr​​oid Retreiving JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序,使一个API调用一个PHP脚本,回声JSON对象。测试PHP文件通过浏览器手动返回预期的信息,但我的应用程序充当如果返回的字符串是空的(之前,我什至解码JSON对象的点)。

下面是我的code的片段。我用这个脚本在我的应用程序中多次成功地为API的呼应琴弦。

 字符串urlParameters =
    请求= item_search&安培; item_num =+酒吧code +&放大器; OU =+ OU +&放大器; user_tag =+字母+&放大器;版本=+版本+&放大器; scan_point =回报;
网址URL = NULL;
尝试{
        如果(TESTMODE ==真)
        {
            URL =新的URL(HTTP://URL/api.php);
        }
        其他
        {
            URL =新的URL(HTTP://URL/api.php);
        }
    }
    赶上(MalformedURLException的E)
    {
        e.printStackTrace();
    }
    StringBuilder的输出=新的StringBuilder();
    尝试
    {
        断言网址!= NULL;
        康涅狄格州的URLConnection = url.openConnection();
        conn.setDoOutput(真);
        OutputStreamWriter作家=新OutputStreamWriter(conn.getOutputStream());
        writer.write(urlParameters);
        writer.flush();
        writer.close();
        串线;
        读者的BufferedReader =新的BufferedReader(新的InputStreamReader(conn.getInputStream()));
        而((行= reader.readLine())!= NULL)
        {
            output.append(线);
        }
        writer.close();
        reader.close();
    }
    赶上(IOException异常E)
    {
        e.printStackTrace();
    }
    串outputString = output.toString();


解决方案

你试过 OkHttp。


  

HTTP的方式是现代应用程序的网络。这是我们如何进行数据交换和放大器;媒体。这样做HTTP有效使你的东西加载速度更快,节省带宽。


您可以尝试<一个href=\"https://raw.github.com/square/okhttp/master/samples/guide/src/main/java/com/squareup/okhttp/guide/GetExample.java\"相对=nofollow>以下code :

 包com.squareup.okhttp.guide;进口com.squareup.okhttp.OkHttpClient;
进口com.squareup.okhttp.Request;
进口com.squareup.okhttp.Response;
进口java.io.IOException异常;公共类GetExample {
  OkHttpClient客户端=新OkHttpClient();  串运行(字符串URL)抛出IOException
    请求请求=新Request.Builder()
        的.url(URL)
        。建立();    响应响应= client.newCall(要求).execute();
    返回response.body()字符串()。
  }  公共静态无效的主要(字串[] args)抛出IOException
    GetExample例如=新GetExample();
    串响应= example.run(https://raw.github.com/square/okhttp/master/README.md);
    的System.out.println(响应);
  }
}

欲了解更多,您可以访问:

Vogella的文章

OkHttp 2.0

I am working on an app that makes an API call to a php script that echos a JSON Object. Testing the php file manually through a browser returns the expected information, but my app is acting as if the string that is returned is empty (before I even get to the point of decoding the JSON Object).

Here's the snippet of my code. I've used this script multiple times in my app successfully for api's that echo strings.

String urlParameters = 
    "request=item_search&item_num=" + barcode + "&ou=" + OU + "&user_tag=" + initials + "&version=" + version + "&scan_point=return";
URL url = null;
try {
        if (testMode == true) 
        {
            url = new URL("http://URL/api.php");
        } 
        else 
        {
            url = new URL("http://URL/api.php");
        }
    } 
    catch (MalformedURLException e) 
    {
        e.printStackTrace();
    }
    StringBuilder output = new StringBuilder();
    try 
    {
        assert url != null;
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
        writer.write(urlParameters);
        writer.flush();
        writer.close();
        String line;
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        while ((line = reader.readLine()) != null)
        {
            output.append(line);
        }
        writer.close();
        reader.close();
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
    String outputString = output.toString();

解决方案

Have you tried OkHttp.

HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP efficiently makes your stuff load faster and saves bandwidth.

You can try following code:

package com.squareup.okhttp.guide;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;

public class GetExample {
  OkHttpClient client = new OkHttpClient();

  String run(String url) throws IOException {
    Request request = new Request.Builder()
        .url(url)
        .build();

    Response response = client.newCall(request).execute();
    return response.body().string();
  }

  public static void main(String[] args) throws IOException {
    GetExample example = new GetExample();
    String response = example.run("https://raw.github.com/square/okhttp/master/README.md");
    System.out.println(response);
  }
}

For more you can visit:

Vogella's article

OkHttp 2.0

这篇关于从URL的Andr​​oid Retreiving JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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