Tag: android

从httpresponse android处理json的最佳方法

我用httpclient来调用用django编写的restapi。 它返回了json输出。 我的httpresponse变量存储它,然后将响应转换为字符串,然后转换为json对象,我认为它虽然很有用但很长。 我是java的新手,任何人都可以告诉我,下面代码的最佳替代逻辑是什么 public void onClick(View v) { // TODO Auto-generated method stub HttpClient httpclient = new DefaultHttpClient(); HttpGet httppost = new HttpGet(“http://10.0.2.2:8000/api/ca/entry/? format=json&username=pragya”); try { // Add your data //List nameValuePairs = new ArrayList(2); //nameValuePairs.add(new BasicNameValuePair(“username”, un.getText().toString())); //nameValuePairs.add(new BasicNameValuePair(“username”, pw.getText().toString())); //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); InputStream is = […]

找不到适合ArrayAdapter的构造函数(MainListActivity.GetBlogPostsTask,int,String )

我在运行此代码时遇到此错误,我正在关注树屋构建一个博客阅读器Android应用程序 ,现在我收到此错误 错误:(120,52)错误:没有为ArrayAdapter找到合适的构造函数(MainListActivity.GetBlogPostsTask,int,String [])构造函数ArrayAdapter.ArrayAdapter(Context,int,int)不适用(参数不匹配; MainListActivity.GetBlogPostsTask无法转换到Context)构造函数ArrayAdapter.ArrayAdapter(Context,int,String [])不适用(参数不匹配; MainListActivity.GetBlogPostsTask不能转换为Context)构造函数ArrayAdapter.ArrayAdapter(Context,int,List)不适用(参数不匹配; MainListActivity.GetBlogPostsTask无法转换为Context) 现在我在这段代码中遇到错误 private void udpateList() { if(blogData == null){ // TODO: handle error }else{ try { JSONArray jsonPosts = blogData.getJSONArray(“posts”); blogPostTitles = new String[jsonPosts.length()]; for (int i = 0; i < jsonPosts.length(); i++){ JSONObject post = jsonPosts.getJSONObject(i); String title = post.getString("title"); title = Html.fromHtml(title).toString(); blogPostTitles[i] = title; } […]

`getExternalStorageDirectory()`问题 – Android

我正在编写一个Android应用程序,它需要一些图片,并希望将它们全部保存在与我的应用程序相关的唯一目录中。 此目录应该可以从标准库中访问,以便用户以后可以(当应用程序不一定运行时)检查拍摄的图片。 我的问题是,每个不同的手机供应商,具有不同的Android版本,具有不同的图库路径。作为一个例子: Environment.getExternalStorageDirectory() + Environment.DIRECTORY_PICTURES +”/myFolder” 将运行Samsung Galaxy Nexus运行android 4.1.1 ,并在Asus Transformer Pad运行android 4.0.3 ,但不在HTC Desire运行android 2.3.5 。 这将导致我的应用程序在尝试在指定路径中保存新目录时崩溃,如下所述: boolean success = false; myFolder = new File( Environment.getExternalStorageDirectory() + Environment.DIRECTORY_PICTURES + “/myFolder” ); if( myFolder.exists() ){ //do nothing }else{ success = dvaFolder.mkdir(); if( success ){ // Do something on success /* * folder has […]

如何在EditText提示后设置焦点到文本?

我感兴趣的是你可以在提示EditText之后将焦点设置为文本? 如果xml布局没有这样的属性? 在那时我仍然看起来像这样。 需要 编辑: Asok给出了最快和最有效的答案 我也发现了类似的方式: EditText.append( “60”); // 60或EditText中的文本

Android:进度条将对象上传到服务器

我想在将对象发送到服务器时放入进度条。 但我不知道我需要在doInBackground()方法中编写什么代码以及onProgressUpdate()中的代码。 这里Client Socket程序用于将Object发送到服务器。 代码(内部类): class DownloadFileAsync extends AsyncTask { @Override protected void onPreExecute() { dialog = new ProgressDialog(this); dialog.setMessage(“Uploading…”); dialog.setIndeterminate(false); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setProgress(0); dialog.show(); } @Override protected String doInBackground(String… aurl) { } @Override protected void onProgressUpdate(String… progress) { } @Override protected void onPostExecute(String unused) { } } 所以,这里我需要在doInBackground()和onProgressUpdate()中编写 我有一个对象serverObject ,我需要将其发送到服务器。 目前我正在使用以下代码将对象发送到服务器而没有任何进度条: Socket s = new […]

自定义HTTP方法在Retrofit 2中不起作用

您好我在我的代码中使用了自定义方法,如下所示,但它总是给我 java.lang.IllegalArgumentException:方法AUTH必须没有请求体。 我的代码不工作总是说: Custom method AUTH, must not have a Body @Headers(“Content-Type: application/json”) @HTTP(method = “AUTH”, path = “login/{deviceId}”, hasBody = true) Call getLogin( @Path(“deviceId”) int deviceId, @Body RequestBody password); 我正在使用以下依赖。 compile ‘com.google.code.gson:gson:2.8.1’ compile ‘com.squareup.retrofit2:retrofit:2.3.0’ compile ‘com.squareup.retrofit2:converter-gson:2.3.0’ compile ‘com.squareup.okhttp3:okhttp:3.8.1’ compile ‘com.squareup.okhttp3:logging-interceptor:3.4.1’ 你能帮我么。

AsyncTask(异步进程)

我有关于异步任务的问题 来自android的Async 使用2个活动“A”和“B” 通过输入一个单词从URL搜索并在DTO中存储值,然后从getter和setter中获取值。 我的复杂性是我在我的活动“B”中实现了异步,并且该活动从同一个DTO获取值。 问题是,我的post知道在后台做的事情已经从DTO和DTO取得价值从互联网上取得价值……如果互联网连接速度慢的话。 我将意图从“A”发送到“B”并在“B”上显示结果 问题: 1.如果我删除异步,则应用程序显示黑页并冻结(仅在连接速度较慢的情况下)但显示数据 2.如果我使用aync,那么有时会显示进度对话框很长时间,并且知道数据已经在UI中显示 代码链接https://www.dropbox.com/s/p27rpokz68sryv3/SearchData.java https://www.dropbox.com/s/rm3i52djiay327u/SearchData_DTO.java https://www.dropbox.com/s/2hpufx2a12480on/Search.java 请建议我可能的解决方案 问候

如何创建简单的Web服务

如何使用java创建可由移动应用程序(Android / iPhone应用程序)使用的简单Web服务。 请提出解决方案。

R.java在做Android教程时没有生成?

所以我正在研究android.com提供的Android教程。 但是,当我到达添加操作按钮部分时,我尝试使用支持Android 2.1的代码和支持库,这似乎导致.xml文件中的错误。 我有相同的.xml名称,“main_activity_actions.xml”具有相同的确切代码。 我记得我的R.java然后,每当我拿出时都能生成 所以,我不确定这有什么问题。 到目前为止,我的代码在整个教程中基本相同。 教程链接: http : //developer.android.com/training/basics/actionbar/adding-buttons.html 整个代码:

获取特定像素的x,y位置

我遇到了一个问题,我得到了一个代码,它读取所有像素并获取像素的RGB颜色。 但在此之前,我将图片切成块,这样我也可以计算出更大的图像,而不会超出内存。 这是代码: Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); try { decoder_image = BitmapRegionDecoder.newInstance(filePath, false); } catch (IOException e) { e.printStackTrace(); } try { boolean bool_pixel = true; final int width = decoder_image.getWidth(); final int height = decoder_image.getHeight(); // Divide the bitmap into 1100×1100 sized chunks and process it. // This makes sure that the app will not be “overloaded” […]