LeoHirasawa 学生  2014年08月22日
从接口获取数据出现问题(新手求助~)
想做一个天气预报的功能,所以尝试着从网上的接口获取JSON数据,有一个在网上COPY下来的sycnhttp类,一个mainActivity,按照网上的教程先启动了tomcat,然后运行程序,结果出了问题,显示Toast为“未找到”,说明抛出了异常(经过我用Toast测试,在String retStr = syncHttp.httpGet(urlStr, paramsStr);这句时就无法执行了,但是可以进入try语句块,说明与后面对JSON的解析应该没什么关系。。应该就是这句话进行不下去,数据应该是没获取到),若水的教程把一个WAR包放到了tomcat的webapps文件夹中,我不会web开发什么的,小白不懂这个有用没?简单的从接口获取数据需要这个东西吗?这个天气接口,在浏览器上直接输入是有效的,但是在程序中无法获取到数据。小白求大神帮忙看看,到底是哪里出了问题呢??跪求大神啦,这任务比较紧

sycnhttp类

package com.leo.http;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.apache.http.params.HttpConnectionParams;

import android.R.integer;


public class SyncHttp
{


private static final int TIME_OUT = 1000 * 6; // 超时
private static final String METHOD_POST = "POST";
private static final String METHOD_GET = "GET";
private static final int HTTP_OK = 200;
private final String CHARTSET = "UTF-8"; //字符编码
private final int BUFFER = 1024 * 8;//缓冲区
public String httpGet(String urlStr, String paramsStr) throws Exception
{

StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append(urlStr);
if (null!=paramsStr&&!paramsStr.equals(""))
{
urlBuilder.append("?");
urlBuilder.append(paramsStr);
}
//Log.i(Constants.TAG, urlBuilder.toString());
URL url = null;
HttpURLConnection conn = null;
InputStream inStream = null;
String response = null;
try
{
url = new URL(urlBuilder.toString());
System.out.println("");
//根据URL打开远程连接
conn = (HttpURLConnection) url.openConnection();
//设置参数
conn.setDoInput(true);
conn.setConnectTimeout(TIME_OUT);
conn.setRequestMethod(METHOD_GET);
conn.setRequestProperty("accept", "*/*");
//建立连接
conn.connect();
//接受返回码
int responseCode = conn.getResponseCode();
if (responseCode == HTTP_OK)
{
//获取输入流
inStream = conn.getInputStream();
//从输入流中获取信息
response = getResponse(inStream);
}
else
{
//请求失败
response = "返回码:"+responseCode;
}
} catch (Exception e)
{
throw e;
}
finally
{
//关闭连接
conn.disconnect();
}
return response;
}

private String getResponse(InputStream inStream) throws IOException
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int len = -1;
byte[] buffer = new byte[BUFFER];//缓冲区
while((len=inStream.read(buffer))!=-1)
{
outputStream.write(buffer, 0, len);
}
byte[] data = outputStream.toByteArray();
return new String(data);
}
}


mainActivity

package com.leo.http;

import org.json.JSONArray;
import org.json.JSONObject;

import android.R.string;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getweather();
}

private void getweather()
{
String urlStr = "http://api.k780.com:88/";
String paramsStr = "app=weather.future&weaid=1&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
SyncHttp syncHttp = new SyncHttp();

try
{
String retStr = syncHttp.httpGet(urlStr, paramsStr);//这句执行不了
Toast.makeText(MainActivity.this,"ok", Toast.LENGTH_LONG).show();

JSONObject jsonObject = new JSONObject(retStr);
String retstring1 = jsonObject.getString("success");

if (retstring1=="0")
{

JSONArray list = jsonObject.getJSONArray("results");

for (int i = 0; i < 1; i++)//测试,所以只读取数组的第一项
{
JSONObject weatherlist = (JSONObject)list.opt(i);
String date = weatherlist.getString("days");
String weather = weatherlist.getString("week");
String wind = weatherlist.getString("temperature");
String temperature = weatherlist.getString("weather");
Toast.makeText(MainActivity.this,"OK!", Toast.LENGTH_LONG).show();
}

}

else
{
//Toast.makeText(MainActivity.this, "未找到!", Toast.LENGTH_LONG).show();//被执行的不是这个toast,将其注释掉了还是会出现“未找到”
}

}

catch (Exception e)
{
e.printStackTrace();
Toast.makeText(MainActivity.this, "未找到!", Toast.LENGTH_LONG).show();//在此处接收到了异常,这个toast被执行了
}

}
}


在运行程序前启动了tomcat


接口在浏览器可实现内容
http://api.k780.com:88/?app=weather.future&weaid=1&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json

下面是LOGCAT(觉得报错的内容好像和我出现的问题没什么关联。。)

...全文
169 点赞 收藏 11
写回复
11 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Android
创建于2009-10-09

4.6w+

社区成员

9.0w+

社区内容

移动平台 Android
社区公告
暂无公告