我正在使用webservice来检索JSON并将其转换为字符串。但我得到的字符串为null。
MainActivity类是:
package com.example.webserviceeg;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btnshowdata;
TextView txtdata;
String URL="http://www.footballultimate.com/fifa/index.php/api/matchShedule/";
ProgressBar pb;
HttpResponse response;
String result;
HttpEntity entity;
InputStream is;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnshowdata=(Button) findViewById(R.id.btnshowdata);
txtdata=(TextView) findViewById(R.id.txtdata);
pb=(ProgressBar) findViewById(R.id.progressBar1);
btnshowdata.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Retrievevalues().execute();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class Retrievevalues extends AsyncTask<String,Void,Void>
{
@Override
protected void onPreExecute() {
pb.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(String... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
// TimeZone tz = TimeZone.getDefault();
// Date now = new Date();
// int offsetFromUtc = tz.getOffset(now.getTime()) / 1000;
pairs.add(new BasicNameValuePair("time_offset","19800"));
httppost.setEntity(new UrlEncodedFormEntity(pairs));
response = httpclient.execute(httppost);
result=responsetostring.getResponseBody(response);
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
// txtdata.setText(result);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
pb.setVisibility(View.INVISIBLE);
}
}
}
我正在使用包含静态方法的第二个类将响应对象转换为字符串:
responsetostring.java
package com.example.webserviceeg;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.protocol.HTTP;
public class responsetostring {
// Json Response Processing
// Json Response Processing
public static String getResponseBody(HttpResponse response) {
// TODO Auto-generated method stub
String response_text = null;
HttpEntity entity = null;
try {
entity = response.getEntity();
try {
response_text = _getResponseBody(entity);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return response_text;
}
public static String _getResponseBody(final HttpEntity entity) throws IOException,
ParseException {
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
InputStream instream = entity.getContent();
if (instream == null) {
return "";
}
if (entity.getContentLength() > Integer.MAX_VALUE) {
throw new IllegalArgumentException(
"HTTP entity too large to be buffered in memory");
}
String charset = getContentCharSet(entity);
if (charset == null) {
charset = HTTP.DEFAULT_CONTENT_CHARSET;
}
Reader reader = new InputStreamReader(instream, charset);
StringBuilder buffer = new StringBuilder();
try {
char[] tmp = new char[1024];
int l;
while ((l = reader.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
} finally {
reader.close();
}
return buffer.toString();
}
public static String getContentCharSet(final HttpEntity entity)
throws ParseException {
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
String charset = null;
if (entity.getContentType() != null) {
HeaderElement values[] = entity.getContentType().getElements();
if (values.length > 0) {
NameValuePair param = values[0].getParameterByName("charset");
if (param != null) {
charset = param.getValue();
}
}
}
return charset;
}
}
但是字符串导致MainActivity,我将其作为null。请帮助
答案 0 :(得分:0)
更改为:
if (instream != null) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
return sb.toString();
}
else
{
return "";
}