English 中文(简体)
Java 正在读取 http 上 php 的 http 请求中的 JSON JSON - 错误请求 400
原标题:Java Reading JSON from http request on php - Bad Request 400
  • 时间:2012-05-23 15:14:48
  •  标签:
  • java
  • php
  • json

Im willing to read a simple json file from a http request. This is the URL im tryng to read:

http://tccdahora.servehttp.com/state.php" rel=“no follow'>http://tccdahora.servehttp.com/state.php

(笑声) 因此,我找到了一种方法将一个 URL 转换成JSON的方法, 但是它不起作用, 尝试了许多不同的方法, 或者让一个头头上岗, 但是没有成功。

这是方法 :

public static JSONArray getPostJSONObject(String url) throws Throwable {

    // faz o POST na pagina php e obtem o inputstream de resposta
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url); // "http://tccdahora.servehttp.com/teste.php"
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    httppost.setHeader("host", url);
    //httppost.setHeader("Content-Type", "application/json");
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();

    // transforma o que o php printar em uma strigzona
    JSONArray jArray;
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    sb.append(reader.readLine() + "
");

    String line = "0";
    while ((line = reader.readLine()) != null) {
        sb.append(line + "
");
    }
    is.close();
    String result = sb.toString();
    Log.e("", result);
    // transformando em uma array de objetos JSON
    jArray = new JSONArray(result);
    return jArray;
}

是否我做错了什么? Im realize 错误请求错误, 文章的字符串正在返回 HTML 代码, 而不是我浏览器显示的 Json 。

非常感谢大家的注意!

问题回答

删除以下行 :

httppost.setHeader("host", url);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签