【问题标题】:error while Creating REST client in android在android中创建REST客户端时出错
【发布时间】:2015-03-24 08:57:53
【问题描述】:

1) 在 netbeans 中使用 json 实现 web 服务,从 MySQL 数据库中获取数据

2)在android中创建rest客户端来调用web服务

修改后的代码

我的清单文件

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

jsonParse.java

package android.project.srt.demojson;

public class JsonParser {

 static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JsonParser() {
}

public String getJSONFromUrl(String url) {
    // Making HTTP request
    try {
        HttpGet get = new HttpGet(url);
        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(get);
        HttpEntity entity = response.getEntity();

        InputStream inputStream=entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream),8 * 1024);

        String line=null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    inputStream.close();
    } catch (UnsupportedEncodingException e) {
        Log.e("unsupported ","encoding exception" +e.getMessage());
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        Log.e("clientprotocolexception ","" + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        Log.e("io","Exception " + e.getMessage());
        e.printStackTrace();
    }

    return sb.toString();
}
}

MainActivity.java

public class MainActivity extends ActionBarActivity {

private static String url = "http://192.168.1.31:8084/adminWebservice/mywebservice/SelectRestroTypes";
String categoryname;
TextView tv1;

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv1 = (TextView) findViewById(R.id.textView1);

    //new JSONParse().execute();

    pDialog = new ProgressDialog(MainActivity.this);
    pDialog.setMessage("Loading Data ...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.show();
    tv1.setText("Getting Values Pls wait..");

    JsonParser jParser = new JsonParser();
    // Getting JSON from URL
//    JSONObject json = jParser.getJSONFromUrl(url);

    String jsonString=jParser.getJSONFromUrl(url);
    tv1.setText(jsonString);
}    
}

logcat 仅显示此错误:

03-24 19:11:20.914  30629-30629/android.project.srt.demojson E/ViewRootImpl﹕ sendUserActionEvent() mView == null

欢迎任何关于在 android 中创建 json 客户端的建议

提前感谢您的帮助

【问题讨论】:

  • 这是哪一行? MainActivity.java:114
  • 与您的代码无关,而是一般建议,您应该使用 Retrofit 在您的 android 应用程序中拥有一个不错的 REST API。它功能强大,经过良好测试且直截了当。 square.github.io/retrofit
  • 114 行:categoryname = json.getString("CategoryName");
  • @Hugo Gresse,谢谢,也会尝试改造
  • 你瞎了吗? ...http://localhost:8084 refused... 或者你阅读有问题?服务器在设备/模拟器上吗?

标签: java android json web-services


【解决方案1】:

看来这是你的问题

http://localhost:8084 的异常连接被拒绝

【讨论】:

  • 该 url 应该指向您的 web 服务运行的服务器地址,而不是 localhost,如果中间有防火墙,端口 8084 应该打开。
  • 看看我的代码,我在 MainActivity.java 中的 URL 是:“192.168.1.31:8084/adminWebservice/mywebservice/…
  • 我没有看到旧的错误现在好像你已经编辑了这个问题,现在看到的错误是不同的,你有没有试过在网上搜索我看到很多关于这个问题的链接[链接]@987654323 @
  • 好的,因为我在 samsung s3 中运行该应用程序,这是一个错误,谢谢@Sameer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-02
  • 1970-01-01
  • 2014-03-10
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多