80,351
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
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被执行了
}
}
}